簡體   English   中英

Spring 引導驗證給出錯誤請求

[英]Spring Boot Validation gives Bad Request

我的POM:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-validation</artifactId> 
</dependency>

我的Controller:

@RestController @Validated

方法名(@Valid @RequestBody POJO_NAME)

我的POJO

@Pattern(regexp="^[a-zA-Z0-9]{30}",message="uniqueId length must be 30")
    private String uniqueId;

    @Pattern(regexp="^[a-zA-Z0-9]{50}",message="authId length must be 50")
    private String authId;

每當我嘗試點擊發布請求(有和沒有有效值)時,我都會在 postman 以下

{
    "timestamp": "2022-11-24T16:51:26.733+00:00",
    "status": 400,
    "error": "Bad Request",
    "path": "path/to/post/request"
}

請幫助我找出我的代碼中缺少什么阻止驗證按預期工作。

評論中提到的 curl 請求看起來不正確,這就是錯誤請求(狀態代碼 - 400)出現的原因。 我可以看到-data-raw后缺少大括號 ({})

請嘗試使用此 curl 請求

curl --location --request POST 'localhost:8080/servicecatalogue/ECOM/1234/inquiry' \
--header 'Content-Type: application/json' \
--data-raw '{
    "authId": "userId",
    "authPassword": "************",
    "serviceId": "1984120645",
    "uniqueId": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
    "serviceAmount": 100,
    "quantity": 2
}'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM