Java Spring MVC
When sending invalid requests (broken JSON for example) to the controller endpoint, the response is:
HTTP status 400: "The request sent by the client was syntactically incorrect" this is returned without actually entering the controller action.
since my endpoint is open for 3rd parties I want to override this error code (only this kind of error - not all error codes from the endpoint) and modify it.
Overriding the message is optional but would be a nice addition.
** Explain for the duplicate mark: I know how to set the error code once the request lands in the controller action, the issue i am describing is that the request does not enter the action method since the syntax is incorrect.
I think this kind of error message is MethodArgumentNotValidException.class . you can use an controller adviser and change returned error code :
@ControllerAdvice
public class MyErrorHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.OK /*or your optional error code */ )
@ResponseBody
public JPresent myHandler(MethodArgumentNotValidException ex) {
// handle error
}
add this code in you web.xml file
<error-page>
<error-code>400</error-code>
<location>/WEB-INF/views/errorPages/page-error-400.jsp</location>
</error-page>
you can do the same thing for the rest of error code you want to handle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.