首页 > 解决方案 > 对于 400 Bad Request 没有得到正确的错误信息

问题描述

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

@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");
}
}

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

我们得到的回应——

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

但预期是 -

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

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

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

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

标签: javaspringspring-boot

解决方案


我刚刚更新了 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


推荐阅读