【问题标题】:Spring custom message in 404 exception404异常中的Spring自定义消息
【发布时间】:2014-09-28 13:09:58
【问题描述】:

我的控制器中有一个方法,如下所示:

@RequestMapping(value = VideoSvcApi.VIDEO_DATA_PATH, method = RequestMethod.POST)
public @ResponseBody
VideoStatus setVideoData(
        @PathVariable(VideoSvcApi.ID_PARAMETER) long id,
        @RequestParam(value = VideoSvcApi.DATA_PARAMETER) MultipartFile videoData,
        HttpServletResponse response) {
    Video video = null;
    for (Video v : videos) {
        if (v.getId() == id) {
            video = v;
            break;
        }
    }
    if (video == null) {
        throw new VideoNotFoundException(id);
    } else {
        try {
            videoFileManager.saveVideoData(video,
                    videoData.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

自定义异常如下所示:

@ResponseStatus(value = HttpStatus.NOT_FOUND)
private class VideoNotFoundException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    public VideoNotFoundException(long id) {
        super("Video with id " + id + " not found");
    }
}

当我遇到一个不存在 id 的路径时,响应是这样的:

{
    "timestamp":1407263672355,
    "error":"Not Found",
    "status":404,
    "message":""
}

我的问题是...如何在响应中设置自定义消息,但保留 json 结构的其余部分?

我知道我可以在注释中使用“原因”属性(在自定义异常中),但是这样做我总是会返回相同的消息,并且我想显示如下消息:“ID X 的视频不是找到”

谢谢!

【问题讨论】:

    标签: java json spring exception http-status-code-404


    【解决方案1】:

    ResponseStatus 有一个您可以使用的 reason 属性。

    【讨论】:

    • 感谢您的回答,是的,正如我在回答中所说的那样。我知道我可以在注释中使用“原因”属性(在自定义异常中),但这样做我总是会返回相同的消息,我想显示如下消息:“找不到 id X 的视频”
    • 如何为您的特定异常创建一个@ExceptionHandler 并从异常构建生成的字符串。
    • 我试过了,但是我想保持相同的json结构,如果我按照你说的做,我不知道如何获取时间戳..再次感谢
    • 您可以创建自己的对象,该对象具有时间戳、错误、状态和消息的属性,并从您的异常处理程序中返回。
    • 现在我明白你的意思了。我会努力的,谢谢!
    【解决方案2】:

    这就是我最终做的事情..

    private class VideoNotFoundException extends RuntimeException {
    
        private static final long serialVersionUID = 1L;
    
        public VideoNotFoundException(long id) {
            super("Video with id " + id + " not found");
        }
    
    }
    
    @SuppressWarnings("unused")
    private class VideoNotFoundExceptionMessage {
    
        private long timestamp;
        private String error;
        private int status;
        private String exception;
        private String message;
    
        public VideoNotFoundExceptionMessage() {
            // constructor
        }
    
        public long getTimestamp() {
            return timestamp;
        }
    
        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }
    
        public String getError() {
            return error;
        }
    
        public void setError(String error) {
            this.error = error;
        }
    
        public int getStatus() {
            return status;
        }
    
        public void setStatus(int status) {
            this.status = status;
        }
    
        public String getException() {
            return exception;
        }
    
        public void setException(String exception) {
            this.exception = exception;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
    }
    
    @ExceptionHandler(VideoNotFoundException.class)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    private VideoNotFoundExceptionMessage VideoNotFoundExceptionHandler(VideoNotFoundException e) {
        e.printStackTrace();
    
        VideoNotFoundExceptionMessage message = new VideoNotFoundExceptionMessage();
        message.setTimestamp(new Date().getTime());
        message.setError("Not Found");
        message.setStatus(404);
        message.setException(e.getClass().getCanonicalName());
        message.setMessage(e.getMessage());
        return message;
    }
    

    现在我的回复是这样的:

    {
        "timestamp":1407271330822,
        "error":"Not Found",
        "status":404,
        "exception":"org.magnum.dataup.VideoController.VideoNotFoundException",
        "message":"Video with id 2 not found"
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-26
      • 2013-06-30
      • 1970-01-01
      • 2015-11-04
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多