1

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!

2 Answers 2

1

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"
}
Sign up to request clarification or add additional context in comments.

Comments

0

ResponseStatus has a reason property you could use.

5 Comments

thanks for answering, and yes, as I say in the answer.. 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"
How about creating an @ExceptionHandler for your specific exception and build the resulting String from the exception.
I tried, but I want to keep the same json structure, and if I do what you say, I don´t know how to obtain the timestamp.. thanks again
You could create your own object which has properties for timestamp, error, status and message and return that from your exception handler.
now I understant what you mean.. I will try, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.