Spring 弹簧靴&x2B;REST异常处理程序-始终获取500错误

Spring 弹簧靴&x2B;REST异常处理程序-始终获取500错误,spring,rest,Spring,Rest,我有一个REST服务,它可能引发异常 这是我的自定义例外 public class CommentNotPostableException extends Exception { public CommentNotPostableException(final String message) { super(message); } } 然后,对于我的RESTAPI,我实现了一个RestResponseEntityExcepti

我有一个REST服务,它可能引发异常

这是我的自定义例外

public class CommentNotPostableException extends Exception {

        public CommentNotPostableException(final String message) {
            super(message);
        }

    }
然后,对于我的RESTAPI,我实现了一个
RestResponseEntityExceptionHandler
,它扩展了
ResponseEntityExceptionHandler
其方法之一是

@ExceptionHandler(value = { CommentNotPostableException.class })
    protected ResponseEntity<Object> handleCommentNotPostableException(CommentNotPostableException ex, WebRequest request) {
        StringBuilder builder = new StringBuilder();
        builder.append(ex.getMessage());

        ApiError apiError = new ApiError(HttpStatus.valueOf(46), 
          ex.getLocalizedMessage(), builder.substring(0, builder.length()));
        logger.error("Already posted", ex);
       return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
     }
这是我从日志中得到的

2017-05-29 21:13:32 DEBUG i.b.e.w.r.CommentRestController[57] - dto è CommentDTO [comment=A, vote=3, reservationId=7161]
2017-05-29 21:13:32 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet][181] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is it.besmart.easyparking.exceptions.CommentNotPostableException: Already posted] with root cause
it.besmart.easyparking.exceptions.CommentNotPostableException: Already posted
    at it.besmart.easyparking.web.restcontroller.CommentRestController.postComment(CommentRestController.java:78) ~[classes/:na]

我总是使用@ControllerAdvice来实现这一点,它工作得非常完美。参见:示例。他们应该会帮你的。这就是我的工作方式,我的ExceptionHandler类用ControllerAdvice注释,每个异常都用ExceptionHandlerOK注释。我想您应该在这里重写
HttpStatus
enum中的
valueOf(param1)
方法。它总是返回500的原因是
46
HttpStatus
enum中不是有效的Http响应,valueOf()方法正在执行此操作
public statistic HttpStatus valueOf(int statusCode){for(HttpStatus status:values()){if(status.value==statusCode){return status;}}抛出新的IllegalArgumentException(“+statusCode+”);}
您可以按照创建一个返回46状态代码的自定义枚举。谢谢,问题其实是我的自定义响应代码!
{
  "timestamp": 1496084392755,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "it.besmart.easyparking.exceptions.CommentNotPostableException",
  "message": "Already posted",
  "path": "/api/oauth/comment/new"
}
2017-05-29 21:13:32 DEBUG i.b.e.w.r.CommentRestController[57] - dto è CommentDTO [comment=A, vote=3, reservationId=7161]
2017-05-29 21:13:32 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet][181] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is it.besmart.easyparking.exceptions.CommentNotPostableException: Already posted] with root cause
it.besmart.easyparking.exceptions.CommentNotPostableException: Already posted
    at it.besmart.easyparking.web.restcontroller.CommentRestController.postComment(CommentRestController.java:78) ~[classes/:na]