Apologies, I really cannot share the whole code , but will try my best to explain the issue.
I am trying to catch NoHandlerFoundException in Controller Advice with following code :
Controller Advice Code Snippet
@ExceptionHandler({NoHandlerFoundException.class})
public ResponseEntity<ErrorResponse> requestHandlerNotFound(NoHandlerFoundException e, HttpServletRequest request, HttpServletResponse response) {
return new ResponseEntity(new ErrorResponse(e.getMessage(), SomeConstant, SomeConstant), HttpStatus.BAD_REQUEST)
}
Note: I have added @Order(Ordered.HIGHEST_PRECEDENCE) to this class.
application.properties have following properties :
spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=true
Now coming to the issue :
I am getting desired response when I create a GETMapping like :
@GetMapping("/loan/{pathvariable}/somethings")
and try to hit -- http://localhost:8080/loan/123A/something ("s" is missing in the end from somethings) then I get desired error
but when I change from loan to loans then -
@GetMapping("/loans/{pathvariable}/somethings")
-- and try to hit -- http://localhost:8080/loans/123A/something ("s" is missing in the end from somethings) then this is not caught by NohandlerFoundException.
Rather I get:
15:26:58.065 [http-nio-8080-exec-6] DEBUG o.s.web.servlet.DispatcherServlet - GET "/loans/123/something", parameters={}
15:26:58.066 [http-nio-8080-exec-6] DEBUG o.s.d.r.w.RepositoryRestHandlerMapping - Mapped to org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController#followPropertyReference(RootResourceInformation, Serializable, String, PersistentEntityResourceAssembler)
15:26:58.067 [http-nio-8080-exec-6] DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
15:26:58.086 [http-nio-8080-exec-6] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Using @ExceptionHandler org.springframework.data.rest.webmvc.RepositoryRestExceptionHandler#handleNotFound(ResourceNotFoundException)
15:26:58.096 [http-nio-8080-exec-6] DEBUG o.s.w.s.m.m.a.HttpEntityMethodProcessor - Using 'application/octet-stream', given [/] and supported [/]
15:26:58.098 [http-nio-8080-exec-6] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved [org.springframework.data.rest.webmvc.ResourceNotFoundException: EntityRepresentationModel not found]
15:26:58.098 [http-nio-8080-exec-6] DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
15:26:58.098 [http-nio-8080-exec-6] DEBUG o.s.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND
SpringBoot Version : 3.0.0
Please Help !!