【发布时间】:2011-06-24 08:00:06
【问题描述】:
代码是这样的:
@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;
}
}
当我尝试 curl -H "Accept:application/json" -v "http://localhost.com:8080/testerror" 我收到了这个错误: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - 找不到支持返回类型 [class com.kibboko.poprocks.appservices.dtos.ErrorTO] 和 [application/json] 的 HttpMessageConverter
但是如果我尝试 curl -H "Accept:application/json" -v "http://localhost.com:8080/test",工作并返回 json 响应。 “应用程序/xml”也可以。
异常处理程序有什么特别之处我需要处理以便它可以与 json 或 xml 一起使用吗?谢谢!
【问题讨论】:
标签: exception-handling spring-mvc