ExceptionHandler retornando JSON ou XML não está funcionando no spring mvc 3
código é assim:
@Controller
public class TestController {
@RequestMapping(value = "/testerror", method = RequestMethod.GET)
public @ResponseBody ErrorTO testerror(HttpServletRequest request,HttpServletResponse response) {
throw new ABCException("serious error!");
}
@ExceptionHandler(ABCException.class)
public @ResponseBody ErrorTO handleException(ABCException ex,
HttpServletRequest request, HttpServletResponse response) {
response.setStatus(response.SC_BAD_REQUEST);
return new ErrorTO(ex.getMessage());
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody ErrorTO test(HttpServletRequest request,
HttpServletResponse response) {
ErrorTO error = new ErrorTO();
error.setCode(-12345);
error.setMessage("this is a test error.");
return error;
}
}
quando tentei curl -H "Accept: application / json" -v "http://localhost.com:8080/testerror" Eu recebi este erro: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Não foi possível encontrar HttpMessageConverter que suporta o tipo de retorno [class com.kibboko.poprocks.appservices.dtos.ErrorTO] e [application / json]
mas se eu tentar enrolar -H "Accept: application / json" -v "http://localhost.com:8080/test", funcionou e retornou a resposta json. "application / xml" também funcionou.
Existe algo especial sobre o manipulador de exceções que eu preciso cuidar para que ele possa funcionar com json ou xml? Obrigado!