1

i want to handle the error for the following controller method

@GetMapping(value= "search", params = {"id", "!name"})
    public ResponseEntity<?> getMovieById(@RequestParam(name = "id") short id) {
        Movie movie = this.movieService.getMovieById(id);
        if(null == movie) {
            throw new MovieNotFoundException("Unable to find moviee with id: " + id);
        }
        return ResponseEntity.ok(movie);
    }

so if the id in the link nit found i'm throwing MovieNotFoundException.
but spring throw the following error:

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Unable to find com.movies.mmdbapi.model.Movie with id 6; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unable to find com.movies.mmdbapi.model.Movie with id 6
3
  • Hi ! Please elaborate: what softwares are you using, to do what ? The more details you give, the easier it is to give an answer :) Commented Aug 17, 2018 at 0:37
  • What do you mean by softwares ? i'm using spring boot with spring data (repository jpa) and a rest controller . Commented Aug 17, 2018 at 0:40
  • @ScaryWombat it doesn't reach the condition, i put a system.out and doesn't shown Commented Aug 17, 2018 at 0:46

2 Answers 2

4

Your movie object is sent through a ResponseEntity object as the response. And the response is going through HTTP as a JSON object.

So your movie object needs to JSON stringify it itself. Java Reflection API automatically does the job for you. It automatically calls getters in your Movie class and create the JSON object.

However some getters might not return the exact String representation of the relevant variable

Ex:-

public String getUserId() {
    return userId.toHexString();
}

In that case HttpMessageNotWritableException could occur.

So you can use @JsonIgnore annotation above the respective field to ignore it. Or return exact string representation through getters.

Ex:-

@JsonIgnore
private ObjectId userId; 
Sign up to request clarification or add additional context in comments.

2 Comments

Even though this answer is valid consider elaborating a little more to give the OP more insight.
Happy now? Cabrra ;)
0

Use spring's out of the box ControllerAdvice. You don't need to handle your exceptions in Controller class. Just throw any Runtime Exception where-ever it occurs (e.g. Service Component or DAO).

Then write an Error Advice like this:

@ControllerAdvice
class MyErrorAdvice {

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //Response Code your want to return
    @ExceptionHandler({MovieNotFoundException.class})
    public void handleMovieNotFoundException(MovieNotFoundException e) {
        log.error("Exception : ", e);
        // Options lines 
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MyOtherException.class})
    public void handleMyOtherException(MyOtherException e) {
        log.error("Exception : ", e);
        // Options lines 
    }
}

That's it. Hope this helps.

Recently, I have drafted a post on How to - Spring Boot Exceptions Handling.

Comments

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.