0

I am a beginner in Spring REST and was implementing the exception handling in Spring REST. Following is the Controller code which is throwing a custom exception

@RequestMapping(value = "/getAllCountries", method = RequestMethod.GET, headers = "Accept=application/json") public Country getCountries() throws CountryNotFoundException{ throw new CountryNotFoundException(); //List<Country> listOfCountries = countryService.getAllCountries(); // return listOfCountries; } @ExceptionHandler(CountryNotFoundException.class) public ResponseEntity<ErrorMessage> handleException(HttpServletRequest req, Exception e) { ErrorMessage error = new ErrorMessage("Custom handler message "+e.toString(), req.getRequestURI()); return new ResponseEntity<ErrorMessage>(error,HttpStatus.NOT_FOUND); } 

Despite this the handler is not executing and I am getting the below exception:

HTTP Status 500 - Request processing failed; nested exception is org.arpit.java2blog.exception.CountryNotFoundException 

Can someone please let me know are there any other configurations which need to be taken care of for the handler to execute?

EDIT: When I change the parameter for ResponseEntity to String then it works but not with ErrorMessage. Why is this behaviour happening?

6
  • Try changing your return type to String. Commented Jul 2, 2018 at 8:37
  • I guess CountryNotFoundException is not a RuntimeException Commented Jul 2, 2018 at 8:39
  • Could you also try to e.g do the following to check wether your exeptions is being thrown using a couple of different input params? Such as : ResponseEntity<String> handleCountryNotFound(CountryNotFoundException e){ return ResponseEntity.status(404).entity("not found").build()}and then see if it is being called/captured at all? Commented Jul 2, 2018 at 8:41
  • Have you defined CountryNotFoundException to extend Exception? Commented Jul 2, 2018 at 8:49
  • @DforTye no it extends RuntimeException.. Commented Jul 2, 2018 at 9:08

2 Answers 2

3

Try this one

import com.fasterxml.jackson.annotation.JsonProperty; public class ErrorMessage { @JsonProperty private final String message; @JsonProperty private final String requestURI; public ErrorMessage(String message, String requestURI) { this.message = message; this.requestURI = requestURI; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

This is awesome. In all the examples I have come across I didn't see such annotations for handlers. Is this version specific of spring?
No, It isn't version specific. Spring tries to convert ResponseEntity<?> to response using HttpMessageConverter. To tell Jackson that ErrorMessage can be serialized to json we use @JsonProperty.
0

Please follow my post on exception handling. I've created sample project for more detailed reference which can found here

1 Comment

thanks for your answer. When i change the parameter of ResponseEntity to string instead of a custom error class, it runs properly. But for custom error message parameter it does not. can you please let me know why?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.