为什么500错误返回json消息值为空



Spring引导应用程序有一个类似以下的方法

getBasicInfoList(@RequestParam int pageNum, @RequestParam @Max(20)int pageSize)

pageSize不能超过20,如果超过20,则返回

{
"timestamp": 1602562522208,
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/convertibleBond/basicInfos"
}

和应用程序抛出异常

javax.validation.ConstraintViolationException: getBasicInfoList.pageSize: must be less than or equal to 20
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:117) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]

那么为什么message值为空呢?如何让它成为getBasicInfoList.pageSize: must be less than or equal to 20

HTTP 500意味着在服务器日志中查找错误

幸运的是,你做到了:

javax.validation.ConstraintViolationException: getBasicInfoList.pageSize: must be less than or equal to 20
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:117) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]

这就是问题所在。你需要修复它。

Q: 那么,为什么(JSON(消息值为空呢?

A: 因为服务器端应用程序在有机会写Json回复之前就崩溃了。就这么简单。

修复Spring Boot(服务器端(约束错误,您将再次获得HTTP(Json(消息响应。

PS:

您还可以考虑在Spring Boot应用程序中添加异常处理。例如:使用Spring Boot 进行REST API错误处理

相关内容

  • 没有找到相关文章

最新更新