我希望您能动态返回HTTP状态码,例如400、400、404等,根据响应对象错误。我被引用到这个问题-Programmatically change http response status using spring 3 restful,但它没有帮助我。
我有一个控制器类,其中包含一个
我有一个控制器类,其中包含一个
@ExceptionHandler
方法。@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseEntity<?> handleException(CustomException e) {
return new ResponseEntity<MyErrorResponse>(
new MyErrorResponse(e.getCode(), ExceptionUtility.getMessage(e.getMessage())),
ExceptionUtility.getHttpCode(e.getCode()));
}
ExceptionUtility
是一个类,我在这个类中有两个方法被用到(getMessage
和getCode
)。
public class ExceptionUtility {
public static String getMessage(String message) {
return message;
}
public static HttpStatus getHttpCode(String code) {
return HttpStatus.NOT_FOUND; //how to return status code dynamically here ?
}
}
我不想检查条件并根据响应代码返回结果,有没有其他更好的方法来做到这一点?