java - 对于 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,但在异常中,消息以空白(“”)的形式出现。
解决方案
我刚刚更新了 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
推荐阅读
- javascript - 图片标签防止 DOM 操作?
- python - 为什么测试比训练需要更长的时间?
- android - 到 nginx 的 SSL 套接字连接在某些 android 和 ios 设备上不起作用
- javascript - 如何在 Visual Studio Code 预览端打开 Web 控制台(如 Chrome 控制台)并动态查看 JavaScript 更改?
- dart - Dose Flutter 或 Dart 有任何等效于 JS 中的 hasProperty() 方法或 PH 中的 isset 方法的方法
- javascript - 在 TypeScript 上使用 ldapjs 的 LDAP 服务器
- javascript - 如何在不使用 eval 的情况下将 JS 数组文字从 HTML 输入字符串转换为数组对象
- python-2.7 - pyspark 数据框将具有两个数组的列更改为列
- javascript - jQuery:循环遍历 20 个不同的“选择”下拉列表以在更改时执行函数
- python - 基于正则表达式的字符串拆分