【问题标题】:Spring Boot Validation gives Bad RequestSpring Boot Validation 给出了错误的请求
【发布时间】:2022-11-26 02:15:49
【问题描述】:

我的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>

我的控制器:

@RestController @验证

方法名(@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;

每当我尝试点击发布请求(有和没有有效值)时,我都会在邮递员下面

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

请帮助我找出我的代码中缺少什么阻止验证按预期工作。

【问题讨论】:

  • 您能否分享两种情况下的 curl 请求?
  • uniqueId 超过 30 个字符的错误卷曲:curl --location --request POST 'localhost:8080/servicecatalogue/ECOM/1234/inquiry' \ --header 'Content-Type: application/json' \ --data-raw '"authId": "userId", “authPassword”:“************”,“serviceId”:“1984120645”,“uniqueId”:“zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz”,“serviceAmount”:100,“数量”:2'

标签: java spring-boot validation pojo bad-request


【解决方案1】:

评论中提到的 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
}'

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 2023-01-13
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2020-03-18
    相关资源
    最近更新 更多