也许我错过了一些东西,但我无法传递一个小问题
我正在构建一个简单的Spring Boot应用程序,并且正在使用MVC Controller:@Controller
注释
我的控制器中有
@GetMapping("/trainee")
public String trainee(@RequestParam("id") long id, Model model) {
要输入该方法,我需要准备以下网址:/trainne?id=1
,而我更喜欢/trainee/1
我知道REST Controller可以使用第二个选项,但是我不能使用它,因为它不支持模板。我可以使用mvc控制器制作一个不错的REST类型网址吗?
我还将添加
@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
答案 0 :(得分:1)
您需要在输入中使用@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
到您的application.properties
,以检查spring为您设置的映射
答案 1 :(得分:1)
是的,您需要使用mvc控制器制作REST类型的url。这是制作REST网址的下面的代码。
@Controller
@RequestMapping("/trainees)
@ResponseBody
@RequestMapping(value="/update/{id}", method=RequestMethod.PUT)
public @ResponseBody MehodName(@PathVariable ("id") Integer id)
{
//write the logic of the method
}