0

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 !!

  • And you expect us to be able to answer this without seeing a controller, configuration etc.? – M. Deinum Mar 28 '23 at 05:09
  • It is a fair point. I will try to make a smaller project and try to replicate this issue and send over all details. Thanks for responding – Manik_Vashisht Mar 28 '23 at 12:35

1 Answers1

0

It is not really a root cause but I was able to workaround and capture the the URL not being captured by NoHandlerFoundException but was rather throwing ResourceNotFoundException

@ExceptionHandler({ResourceNotFoundException.class})
public ResponseEntity<ErrorResponse> requestResourceNotFound(ResourceNotFoundException e, HttpServletRequest request, HttpServletResponse response) {
return new ResponseEntity<>(new ErrorResponse( "No endpoint " + request.getMethod() + " " + request.getRequestURI(),
Constants.INVALID_REQUEST_EXCEPTION_MESSAGE, ErrorLevel.WARN), HttpStatus.BAD_REQUEST);
}

Using this, I was still able to capture the similar NoHandlerFound Exception output.