Spring 带mvc控制器的RESTURL

Spring 带mvc控制器的RESTURL,spring,spring-mvc,spring-boot,url,Spring,Spring Mvc,Spring Boot,Url,也许我错过了什么,但我不能通过一个小问题 我正在构建一个简单的Spring Boot应用程序,我正在使用MVC控制器:@Controller注释 我的控制器里有 @GetMapping("/trainee") public String trainee(@RequestParam("id") long id, Model model) { 要输入该方法,我需要准备如下url:/trainee?id=1,我更喜欢/trainee/1 我知道第二个选项在REST控制器上是可行的,但我不能使用它,因

也许我错过了什么,但我不能通过一个小问题

我正在构建一个简单的Spring Boot应用程序,我正在使用MVC控制器:
@Controller
注释

我的控制器里有

@GetMapping("/trainee")
public String trainee(@RequestParam("id") long id, Model model) {
要输入该方法,我需要准备如下url:
/trainee?id=1
,我更喜欢
/trainee/1

我知道第二个选项在REST控制器上是可行的,但我不能使用它,因为它不支持模板。我可以用mvc控制器创建一个好的REST类型url吗

我还要补充一点

@Controller
@RequestMapping("/trainees")
public class TraineeController {
当我使用@Ken Chan建议的映射时,我收到

2018-12-28 18:49:50.740 DEBUG 5960 --- [nio-8080-exec-5] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 18:49:50.740 DEBUG 5960 --- [nio-8080-exec-5] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 18:49:50.740 DEBUG 5960 --- [nio-8080-exec-5] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 18:49:50.740 DEBUG 5960 --- [nio-8080-exec-5] a.e.w.s.ControllerEndpointHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 18:49:50.740 DEBUG 5960 --- [nio-8080-exec-5] o.s.b.w.s.f.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@6c120aea
//根据@Ben配置添加完全调试

2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] a.e.w.s.ControllerEndpointHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/trainees/trainee/1] are [/**]
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/trainees/trainee/1] are {}
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/trainees/trainee/1] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@470d91ab]]] and 1 interceptor
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] o.s.b.w.s.f.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@3b6c5f54
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/trainees/trainee/1]
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] a.e.w.s.ControllerEndpointHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 21:22:43.113 DEBUG 13352 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /trainees/trainee/1
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/trainees/trainee/1]
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/trainees/trainee/1] are [/**]
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/trainees/trainee/1] are {}
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/trainees/trainee/1] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@470d91ab]]] and 1 interceptor
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/trainees/trainee/1] is: -1
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.b.w.s.f.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@3b6c5f54
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Looking up handler method for path /error
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not find handler method for [/error]
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /error
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] a.e.w.s.ControllerEndpointHandlerMapping : Did not find handler method for [/error]
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/error] is: -1
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@517289db] based on requested media type 'text/html'
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@517289db] in DispatcherServlet with name 'dispatcherServlet'
2018-12-28 21:22:43.128 DEBUG 13352 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Successfully completed request

您需要对输入使用
@PathVariable
注释才能使用url模式匹配

@Controller
@RequestMapping("/trainees")
public class TraineeController {

    @GetMapping("/trainee/{id}")
    public String trainee(@PathVariable("id") String id, Model model) {
         // Your code here
    }

}
您还可以添加
logging.level.org.springframework.web=DEBUG
到您的
应用程序.properties
为了检查spring为您设置了哪些映射

是,您需要使用mvc控制器创建REST类型的url。下面是创建REST url的代码

     @Controller
     @RequestMapping("/trainees)
     @ResponseBody
     @RequestMapping(value="/update/{id}", method=RequestMethod.PUT)
      public @ResponseBody  MehodName(@PathVariable ("id") Integer id)
      {
       //write the logic of the method
       }

这有什么问题:
@GetMapping(value=“/trainee/{id}”
公共字符串培训生(@PathVariable(“id”)long-id,Model-Model)
?@KenChan我已经更新了我的帖子。我不想说你的解决方案有什么问题,但它就是不起作用。我已经尝试过了(很抱歉,我应该提到这个)我已经尝试过了,但它不起作用。我已经在我的帖子的更新中发布了调试。至于我,它找不到模式。嗯,突然它开始起作用了。哦,我不明白。我会再运行一些tests@Fixus也许您没有清理或重建项目,只是使用旧的端点映射运行它?@Fixus还值得注意的是在spring应用程序初始化期间,应该输出所有已注册端点的调试标志