Home > Blockchain >  SpringBoot RestApi call 404 Not Found error
SpringBoot RestApi call 404 Not Found error

Time:04-29

I have a SpringBoot application that I am able to start ( http://localhost:80 ).

In my application.yml :

application:
  baseurl: /respViewer/api

In my application I defined a @RestController and an endpoint :

@RestController
@RequestMapping("${application.baseurl}/viewer")

....

@PostMapping(value = "/getRespList", consumes = MediaType.APPLICATION_JSON_VALUE,
                produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<Object> getResponsibilities(@RequestBody RequestDetail requestDetail)

When I try to access my application using Insomnia or through the Browser I am getting 404 Error.

http://localhost:8080/respViewer/api/viewer/getRespList

{
  "timestamp": "2022-04-25T19:52:32.426 00:00",
  "status": 404,
  "error": "Not Found",
  "message": "",
  "path": "/respViewer/api/viewer/getRespList"
}

I also checked console output and found these messages :

 POST "/respViewer/api/viewer/getRespList", parameters={}
2022-04-25 15:52:32.253 DEBUG 3744 --- [nio-8080-exec-1] o.s.w.s.h.SimpleUrlHandlerMapping        : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2022-04-25 15:52:32.382 DEBUG 3744 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2022-04-25 15:52:32.387 DEBUG 3744 --- [nio-8080-exec-1] o.s.w.s.DispatcherServlet                : Completed 404 NOT_FOUND

What am I doing wrong?

Update 1 :

I added packages location where I have a REST Controller to application.yml :

spring:
      component:
        scan:
          packages: com.example.demoRespManager

and now I am not getting 404 Error, however it doesn't look like I am getting into the body of implementation method. I setup a break-point in the first line of the method but never stop there. An output in the Console is :

2022-04-25 21:25:43.749  INFO 27996 --- [nio-8080-exec-1] o.s.w.s.DispatcherServlet                : Initializing Servlet 'dispatcherServlet'
2022-04-25 21:25:43.750 DEBUG 27996 --- [nio-8080-exec-1] o.s.w.s.DispatcherServlet                : Detected StandardServletMultipartResolver
2022-04-25 21:25:43.915 DEBUG 27996 --- [nio-8080-exec-1] o.s.w.s.DispatcherServlet                : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2022-04-25 21:25:43.915  INFO 27996 --- [nio-8080-exec-1] o.s.w.s.DispatcherServlet                : Completed initialization in 166 ms
2022-04-25 21:25:44.134 DEBUG 27996 --- [nio-8080-exec-1] o.s.w.s.DispatcherServlet                : POST "/respViewer/api/viewer/getRespList", parameters={}
2022-04-25 21:25:44.180 DEBUG 27996 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Using @ExceptionHandler com.<my_package_path>.exception.ApplicationExceptionHandler#handleException(Exception, WebRequest)
2022-04-25 21:25:44.254 DEBUG 27996 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : No match for [application/json], supported: []
2022-04-25 21:25:44.261 DEBUG 27996 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]
2022-04-25 21:25:44.262 DEBUG 27996 --- [nio-8080-exec-1] o.s.w.s.DispatcherServlet                : Completed 415 UNSUPPORTED_MEDIA_TYPE

UPDATE 2 :

In the Header section of the INSOMNIA application I added Content-type = application/json, since I defined it in my endpoint, and after that it started to work. I was able to start an application in the DEBUG mode and when I made a POST request in INSOMNIA I stopped at the first line of my method implementation.

CodePudding user response:

I solved this issue :

  1. updated my properties file with the list of the packages that contain the beans, it's an equivalent of @ComponentScan in Application ( spring.component.scan.packages )
  2. Added Content-type = application/json to the Header in the Insomnia.
  • Related