我有一个如下控制器
@Controller("myController")
@RequestMapping("api")
public class MyController {
@RequestMapping(method = RequestMethod.GET, value = "/get/info/{id}", headers = "Accept=application/json")
public @ResponseBody
Student getInfo(@PathVariable String info) {
.................
}
@ExceptionHandler(Throwable.class)
@ResponseStatus( HttpStatus.EXPECTATION_FAILED)
@ResponseBody
public String handleIOException(Throwable ex) {
ErrorResponse errorResponse = errorHandler.handelErrorResponse(ex);
return errorResponse.toString();
}
}
控制器有一个错误处理机制,在错误处理选项中它总是返回期望失败状态代码417.但我需要根据错误类型设置动态错误Http状态代码,如500,403等。我该怎么做?
答案 0 :(得分:4)
您需要更改输出值 ResponseEntity 的类型。在这里回答: How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?
答案 1 :(得分:3)
我得到一个解决方案,并分享这个,也想知道任何好的建议。
@Controller("myController")
@RequestMapping("api")
public class MyController {
@RequestMapping(method = RequestMethod.GET, value = "/get/info/{id}", headers = "Accept=application/json")
public @ResponseBody
Student getInfo(@PathVariable String info) {
// ...
}
}
// ...
@ExceptionHandler(Throwable.class)
//@ResponseStatus( HttpStatus.EXPECTATION_FAILED)<<remove this line
@ResponseBody
public String handleIOException(HttpServletResponse httpRes,Throwable ex){ // <<Change this
if (some condition) {
httpRes.setStatus(HttpStatus.BAD_GATEWAY.value());
} else {
httpRes.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
ErrorResponse errorResponse = errorHandler.handleErrorResponse(ex);
return errorResponse.toString();
}
预计在休息客户端:
502 Bad Gateway
{
"status":"BAD_GATEWAY",
"error":"java.lang.UnsupportedOperationException",
"message":"Some error message"
}
感谢您的回复。我仍然需要指导好的做法。
答案 2 :(得分:2)
按照上面的代码,你需要更加小心你要抛出和处理的异常。为Throwable设置异常处理程序似乎过于宽泛。
我这样做的方法是使用我的XML / JSON编组注释创建一个ErrorMessage类。
@XmlRootElement(name = "error")
public class ErrorMessage {
private Throwable exception;
private String message;
public ErrorMessage() {
this.message = "";
}
public ErrorMessage(String message) {
this.message = message;
}
public ErrorMessage(Throwable exception) {
this.exception = exception;
this.message = exception.getLocalizedMessage();
}
@XmlTransient
@JsonIgnore
public Throwable getException() {
return exception;
}
public void setException(Throwable exception) {
this.exception = exception;
}
@XmlElement(name = "message")
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
有了这个,我倾向于创建自己的应用程序异常,然后创建我的异常处理程序方法,如:
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorMessage handleResourceNotFoundException(ResourceNotFoundException e, HttpServletRequest req) {
return new ErrorMessage(e);
}
@ExceptionHandler(InternalServerErrorException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorMessage handleInternalServerErrorException(InternalServerErrorException e, HttpServletRequest req) {
return new ErrorMessage(e);
}
有了这些,我只需要从我的控制器方法中抛出适当的异常。例如,如果我抛出一个ResourceNotFoundException,那么Spring会将它重定向到我的handleResourceNotFoundException方法,该方法返回404,并且还将返回表示错误的JSON或XML。
答案 3 :(得分:0)
您可以为您的API使用Aspect。如果为服务定义@Around拦截器,则可以更改响应内容。