XML JAXB Response List of Errors
DougBreaux 270007SMYJ Visits (2386)
Minor addition to Spri
While my earlier-mentioned API supports both JSON and XML, the XML is more legacy, and I hadn't tested all cases with it yet. Further testing revealed that my previous multi-error pattern was too simplistic for XML responses:
List<ErrorResponse> errors = new ArrayList<>();
for (FieldError f: e.g
(Note that's slightly different from my earlier version, I switched the ErrorResponse to use 2 Strings instead of int/String.)
JSON was fine, but "Accept: application/xml" calls were receiving 406 Not Acceptable when errors were returned.
My first thought/fear was that the above Spring @ExceptionHandler approach wasn't going to be smart enough to send the correct response type, based on the caller's "Accept" Request header.
But it turned out it was the much simpler issue that List isn't sufficient to have JAXB serialize out appropriate XML. I really should have remembered this from earlier JAXB endeavors.
(Observation: Spring MVC really is powerful at figuring things out and allowing simple coding patterns.)
The simple solution (with thanks to StackOverflow Why
With the above @ExceptionHandler method modified to:
ErrorList errors = new ErrorList();
for (FieldError f: e.ge
Which produces either the original JSON or the following XML:
No more 406 HTTP response error code.