对于 400 Bad Request 未收到正确的错误消息

Ari*_*Roy 6 java spring spring-boot

我们的控制器看起来像这样 -

@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");
}
}
Run Code Online (Sandbox Code Playgroud)

在这里,如果我们没有在请求中指定所需的参数“testCode”,我们会得到“400 Bad Request”,但错误响应的消息部分仍为空白。

我们得到的回应 -

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"","path":"/test"}
Run Code Online (Sandbox Code Playgroud)

但预计的是——

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"Required String parameter 'testCode' is not present","path":"/test"}
Run Code Online (Sandbox Code Playgroud)

我们正在使用以下 Spring 依赖项 -

<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-framework.version>5.2.6.RELEASE</spring-framework.version>
Run Code Online (Sandbox Code Playgroud)

我所看到的是,我们得到了 MissingServletRequestParameterException,但在异常中消息为空白(“”)。

Ari*_*Roy 7

我刚刚用 更新了 bootstrap.yml server.error.include-message=always。从 Spring 2.3.0 开始,Spring 的默认行为似乎发生了变化。我们可以参考以下链接了解更多详细信息https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content