Skip to content
Advertisement

Spring rest controller @ExceptionHandler return xml content and json errors

So I have a @RestController and I want to return and validate XML based on a schema for a front-end application in order to display them in an editor. I want the errors to be in json format in order to handle and display them with js.

JavaScript

The ServerError I want to return in JSON format :

JavaScript

So the <xml>hello</xml> is returned just fine but when I set the flag to true I get

JavaScript

Also, setting the produces to both XML and JSON yields the same result

JavaScript

Advertisement

Answer

I have managed to solve this by removing the produces from @RequestMapping and specifying with ResponseEntity the type I want to return

JavaScript

The problem with that solution is that all of the methods have an @annotation with the type they produce and this doesn’t, breaking uniformity.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement