Better exception message for missing @RequestBody method parameter

Posted 再见理想

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Better exception message for missing @RequestBody method parameter相关的知识,希望对你有一定的参考价值。

 

https://jira.spring.io/browse/SPR-12888

Description

When I use @RequestBody on one of my controllers for a required parameter, I need some useful way of saying which parameter was missing if it‘s not there. The exception message I see currently looks like this:

 

Required request body content is missing: org.[email protected]a8b8886e
	at org.springframework.test.util.MatcherAssertionErrors.assertThat(MatcherAssertionErrors.java:80)
	at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:92)
	at org.springframework.test.web.servlet.result.JsonPathResultMatchers$1.match(JsonPathResultMatchers.java:56)
	at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:152)
...

 

It would be nice if when that exception could have a reference to the parameter name that was missing, or if the message itself included it instead of the MethodParameter#toString(). I‘ll eventually want to internationalize the message I produce, so the parameter name is preferable.

Activity

 
技术分享Rossen Stoyanchev added a comment - 06/Apr/15 2:57 PM

The HttpMessageNotReadableException can be raised from many places where a method parameter does not apply so adding a new field to the exception isn‘t ideal. I considered sub-classing the exception but what I‘ve done instead is to print the full method signature to improve the error message. Also we now support injecting the selected HandlerMethod into an @ExceptionHandler method so you can obtain the full information on the @RequestMapping method we were trying to call.

技术分享Rossen Stoyanchev added a comment - 06/Apr/15 2:57 PM

Resolving for now but feel free to comment further.

以上是关于Better exception message for missing @RequestBody method parameter的主要内容,如果未能解决你的问题,请参考以下文章

记一次Jenkins 打包异常 ERROR: Exception when publishing, exception message [Failure]

EXCEPTION STACK TRACE: ** BEGIN NESTED EXCEPTION ** javax.net.ssl.SSLException MESSAGE: closing

php错误:Uncaught exception com_exception with message Failed to create COM object

selenium.common.exceptions.TimeoutException: Message: Screenshot: available via screen

使用 SimpleMappingExceptionResolver 时在 spring:message 标签中访问 exception.class.name

python selenium模块使用出错-selenium.common.exceptions.WebDriverException: Message: 'geckodriver'