为什么500错误返回的JSON消息值是空的

huangapple go评论77阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年10月13日 12:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64328466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定