mardi 7 avril 2015

Spring REST service for multiple accept headers is not working

I am trying to create a REST based application using Spring 3.2.0. One of the methods returns a CSV. The code looks like:



@RequestMapping(value = "/employees/{id}/csv", method = RequestMethod.GET, produces = { "text/csv", "application/json" })
public @ResponseBody CSVMessage getCsvData(HttpSession httpSession) {

return getCSV(httpSession);
}


CSVMessage is an implementation of spring HTTP message converter for CSV. My request has headers like



Accept: text/csv,aplication/json


I have a controller advice as follows:



@ControllerAdvice
@Produces("application/api-problem+json")
public class RestErrorHandler {
@ExceptionHandler(InvalidParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public HashMap<String, Object> processPortalWebApplicationException(InvalidParameterException ex) {

return setupMap(Response.Status.BAD_REQUEST.getStatusCode(), "Invalid parameter",
"Please specify the right params", null);
}
}


It works fine when there is no exception and returns a CSV. But when there is an exception I am expecting a JSON representation of the exception from the above exception handler. But that is not happening if the accept header is 'text/csv,aplication/json' but works if I change the order of the accpet headers like 'aplication/json,text/csv'. What am I missing here? How does spring handle accept headers? Or is my approach totally wrong?


Aucun commentaire:

Enregistrer un commentaire