简体   繁体   English

对于 400 Bad Request 没有得到正确的错误信息

[英]For 400 Bad Request not getting proper error message

Our controller looks like this -我们的 controller 看起来像这样 -

@RestController
@RequestMapping(value="api")
@Validated
public class SampleController {
@RequestMapping(value = {"/test"}, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void test(
        @RequestParam(value = "testCode",required=true) String merchantCode
        ) throws Exception{
    System.out.print("This is Test");
}
}

Here if we do not specify the required parameter "testCode" in our request we get "400 Bad Request", but the message section of the error response remains blank.在这里,如果我们没有在我们的请求中指定必需的参数“testCode”,我们会得到“400 Bad Request”,但错误响应的消息部分仍然是空白的。

Response we are getting -我们得到的回应——

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"","path":"/test"}

But expected is -但预期是 -

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"Required String parameter 'testCode' is not present","path":"/test"}

We are using below Spring dependencies -我们在下面使用 Spring 依赖项 -

<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-framework.version>5.2.6.RELEASE</spring-framework.version>

What I have seen is for this we are getting MissingServletRequestParameterException, but in the exception the message is coming as blank("").我所看到的是,我们得到了 MissingServletRequestParameterException,但在异常中,消息以空白(“”)的形式出现。

I just updated the bootstrap.yml with server.error.include-message=always .我刚刚用server.error.include-message=always更新了 bootstrap.yml 。 It appears from Spring 2.3.0 the default behavior of Spring has changed.从 Spring 2.3.0 开始,Spring 的默认行为似乎发生了变化。 We can refer the following link from more details https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content我们可以从更多详细信息中参考以下链接https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content

Configuration in application.yml application.yml 中的配置

server:
  error:
    include-message: always
    include-binding-errors: always
    include-stacktrace: on_trace_param
    include-exception: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM