0

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.

Asaf Maoz
  • 675
  • 3
  • 6
  • 23

2 Answers2

0

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
}
Pasha
  • 1,534
  • 15
  • 27
0

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

BOUALI ALI
  • 230
  • 2
  • 14