Springboot Restapi致电404找不到错误

发布于 2025-01-23 22:10:51 字数 3357 浏览 1 评论 0原文

我有一个可以启动的Springboot应用程序(http:// localhost:80)。

在我的application.yml中:

application:
  baseurl: /respViewer/api

在我的应用程序中,我定义了一个@RestController和一个端点:

@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)

当我尝试使用失眠症或通过浏览器访问应用程序时,我会遇到404错误。

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"
}

我还检查了控制台输出并找到了这些消息:

 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

我在做什么错?

更新1:

添加了包含REST控制器的软件包位置。

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

我 我在方法的第一行中设置了一个断点,但永远不会停止。控制台中的输出是:

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

更新2:

在失眠应用程序的“标头”部分中,我添加了content-type = application/json,因为我在端点中定义了它,之后它开始工作。我能够在调试模式下启动一个应用程序,当我在失眠症中提出帖子请求时,我在方法实现的第一行停止了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2025-01-30 22:10:51

我解决了此问题:

  1. 更新了我的属性文件,其中包含bean的软件包列表,相当于应用程序中的@componentscan(spring.component.scan.scan.packages)
  2. 添加了content-type = application-application/json to application/json to to applions/json失眠。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文