我有一個可以啟動的 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)
當我嘗試使用 Insomnia 或通過瀏覽器訪問我的應用程式時,我收到 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:
我在 application.yml 中添加了我有 REST 控制器的包位置:
spring:
component:
scan:
packages: com.example.demoRespManager
現在我沒有收到 404 錯誤,但是看起來我并沒有進入實作方法的主體。我在方法的第一行設定了一個斷點,但從未停在那里。控制臺中的輸出是:
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:
在 INSOMNIA 應用程式的 Header 部分中,我添加了 Content-type = application/json,因為我在端點中定義了它,然后它開始作業。我能夠在 DEBUG 模式下啟動應用程式,當我在 INSOMNIA 中發出 POST 請求時,我停在了方法實作的第一行。
uj5u.com熱心網友回復:
我解決了這個問題:
- 用包含 bean 的包串列更新了我的屬性檔案,它相當于 Application ( spring.component.scan.packages ) 中的 @ComponentScan
- 在 Insomnia 的 Header 中添加了 Content-type = application/json。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/467201.html
下一篇:如何獲得查詢嵌套評論?
