英文:
Why 500 error return json message value is empty
问题
以下是翻译好的内容:
在Spring Boot应用中有一个类似以下的方法:
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: 必须小于或等于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: 必须小于或等于20
?
英文:
Spring boot app there is a method like below
getBasicInfoList(@RequestParam int pageNum, @RequestParam @Max(20)int pageSize)
pageSize cannot be more than 20, and if more than 20, got below return
{
"timestamp": 1602562522208,
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/convertibleBond/basicInfos"
}
and app throw below exception
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]
So why message
value is empty? How to let it is getBasicInfoList.pageSize: must be less than or equal to 20
?
答案1
得分: 2
HTTP 500意味着请查看服务器日志以获取错误信息!
幸运的是,你已经做到了这一点:
javax.validation.ConstraintViolationException: getBasicInfoList.pageSize: 必须小于或等于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]
这就是问题所在。你需要修复它。
问:那么为什么(JSON)消息值为空?
答:因为服务器端应用在有机会编写Json回复之前崩溃了。就是这么简单。
修复Spring Boot(服务器端)的约束错误,你将再次获得HTTP(Json)消息响应。
附言:
你可能还考虑在你的Spring Boot应用程序中添加异常处理。例如:使用Spring Boot进行REST API错误处理。
英文:
HTTP 500 means look in the server logs for the error!
Fortunately, you've done this:
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]
That's the problem. You need to fix it.
Q: So why (JSON) message value is empty?
A: Because the server-side app crashed before it had the chance to write a Json reply. It's as simple as that.
Fix the Spring Boot (server-side) constraint error, and you'll get HTTP (Json) message responses again.
PS:
You might also consider adding exception handling in your Spring Boot app. For example: REST API Error Handling With Spring Boot
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论