spring-mvc - 未使用路径变量和 http 请求作为参数调用 Spring Rest Controller 方法
问题描述
这是我的控制器方法
@RestController
public class ProfileController {
@GetMapping("/quiz/{quizId}/identifyfromsixjson")
@ResponseBody
UserProfileQuestion playIdentifyFromSix(@PathVariable String quizId, HttpServletRequest request) {
... Calling service method ... here
}
}
应用程序属性
server.contextPath=/myproject
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
因此,当我向http://localhost:8080/myproject/identifyfromsixjson/test发出 GET 请求时,这是我在 Postman 中看到的响应。
{
"timestamp": "2018-10-08T02:42:14.387+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
"path": "/myproject/quiz/test/identifyfromsixjson"
}
启动日志
018-10-08 01:59:32.603 WARN 46035 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-10-08 01:59:32.641 INFO 46035 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/quiz/{quizId}/identifyfromsixjson]}" onto public org.springframework.http.ResponseEntity<com.myproject.model.UserProfileQuestion> com.myproject.controller.ProfileController.fetchUserProfileAndHeadShot(java.lang.String,javax.servlet.http.HttpServletRequest)
2018-10-08 01:59:32.644 INFO 46035 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-10-08 01:59:32.644 INFO 46035 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-10-08 01:59:32.672 INFO 46035 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Mapped URL path [/myproject] onto handler '/myproject'
2018-10-08 01:59:32.678 INFO 46035 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-08 01:59:32.678 INFO 46035 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
我究竟做错了什么?
解决方案
这是您定义的路径:
/quiz/{quizId}/identifyfromsixjson
这是您正在测试的路径
/identifyfromsixjson/test
很明显它们不匹配,这就是您收到该错误的原因。
您可以执行以下操作:
1. 使用您定义的路径进行测试:
http://localhost:8080/myproject/quiz/test/identifyfromsixjson
2. 更新你的路径定义
@GetMapping("/identifyfromsixjson/{quizId}")
@ResponseBody
UserProfileQuestion playIdentifyFromSix(@PathVariable String quizId,HttpServletRequest request) {
... Calling service method ... here
}
然后用
http://localhost:8080/myproject/identifyfromsixjson/test
推荐阅读
- asp.net - 使用什么代替 Azure Web Apps 来允许在应用环境中安装 google chrome?
- c++ - CMake Automoc 错误 1 - 无法编译项目
- c++ - 二进制数据作为命令行参数
- javascript - 如何使用 Puppeteer 单击 Cloud Saas 应用程序上的链接?
- swift - 妥善管理导航栏和标签栏
- xcode8 - 无法在 macOS 10.14 beta 上打开 Xcode 8.3.2
- javascript - objloader 未加载材料
- php - 导航未在我的视频集顶部显示为背景
- java - 将带有字符串的图标作为 XAxisValues 添加到 MPAndroid 图表
- c - 为什么取消引用指针并打印使程序崩溃的值