I'm working with Spring Boot 2.7.1 and trying to implement Exception handling. Somehow it's not working. Here're the details:
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{
@ExceptionHandler(RoomNotFoundException.class)
@ResponseBody
public final ResponseEntity<ErrorMessage> handleRoomNotFoundException(RoomNotFoundException e, WebRequest request) {
ErrorMessage errorMessage=new ErrorMessage(HttpStatus.NOT_FOUND,e.getMessage());
System.out.println("RestResponseEntityExceptionHandler.RoomNotFoundException()");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorMessage);
}
}
RestController Implementation:
@RestController
@RequestMapping("/api")
public class WebServiceController {
@Autowired
private final ReservationService reservationService;
@Autowired
private RoomRepository roomRepository;
@Override
public String toString() {
return "WebServiceController []";
}
public WebServiceController(ReservationService reservationService) {
super();
this.reservationService = reservationService;
}
@RequestMapping (path="/jpa/getroomId/{id}", method=RequestMethod.GET)
public Room getRooms(@PathVariable String id) throws RoomNotFoundException{
Optional<Room> optionalRoom;
try {
optionalRoom= reservationService.getRoomDetail(Integer.parseInt(id));
if(!optionalRoom.isPresent()) {
throw new RoomNotFoundException("Room Id is incorrect:"+id);
}
return optionalRoom.get();
} catch (NumberFormatException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
ErrorMessage Bean class:
public class ErrorMessage {
private HttpStatus status;
private String message;
public ErrorMessage(HttpStatus status, String message) {
super();
this.status = status;
this.message = message;
}
public ErrorMessage() {}
public HttpStatus getStatus() {
return status;
}
public void setStatus(HttpStatus status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
On Postman when I hit: I get no return and the Response status code is 200.
On Eclipse server, I get the below output:
022-07-28 20:55:41.571 INFO 6893 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8000 (http) with context path ''
2022-07-28 20:55:41.575 INFO 6893 --- [ restartedMain] c.k.j.l.LearningSpringApplication : Started LearningSpringApplication in 0.891 seconds (JVM running for 1266.472)
2022-07-28 20:55:41.576 INFO 6893 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
2022-07-28 20:55:49.292 INFO 6893 --- [nio-8000-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-07-28 20:55:49.292 INFO 6893 --- [nio-8000-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-07-28 20:55:49.294 INFO 6893 --- [nio-8000-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
Hibernate: select room0_.room_id as room_id1_2_0_, room0_.bed_info as bed_info2_2_0_, room0_.name as name3_2_0_, room0_.room_number as room_num4_2_0_ from room room0_ where room0_.room_id=?
com.khushboo.java.learningspring.exceptions.RoomNotFoundException: Room Id is incorrect:500
at com.khushboo.java.learningspring.webservice.WebServiceController.getRooms(WebServiceController.java:56)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
Thanks for taking the time and sharing any helpful insights on why the ExceptionHandler code is not getting invoked.