Matias Elorriaga
Matias Elorriaga

Reputation: 9150

Spring custom message in 404 exception

I have a method in my controller, look like this:

@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;
}

and the custom exception look like this:

@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");
    }
}

when I hit some path with an id that does not exists, the response is like this:

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

my question is... how can I set a custom message in the response, but manteining the rest of the json structure?

I know that I can use the "reason" attribute in the annotation (in the custom exception), but doing this I always will return the same message, and I want to display a message like: "Video with id X not found"

Thanks!

Upvotes: 1

Views: 1570

Answers (2)

Matias Elorriaga
Matias Elorriaga

Reputation: 9150

This is what I ended up doing..

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;
}

and now my response looks like this:

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

Upvotes: 1

Patrick Grimard
Patrick Grimard

Reputation: 7126

ResponseStatus has a reason property you could use.

Upvotes: 0

Related Questions