路由与参数
路由与参数
路由
路径匹配
org.springframework.web.bind.annotation.RequestMapping
被用于将某个请求映射到具体的处理类或者方法中:
// @RequestMapping with Class
@Controller
@RequestMapping("/home")
public class HomeController {}
// @RequestMapping with Method
@RequestMapping(value="/method0")
@ResponseBody
public String method0(){
return "method0";
}
// @RequestMapping with Multiple URI
@RequestMapping(value={"/method1","/method1/second"})
@ResponseBody
public String method1(){
return "method1";
}
// @RequestMapping with HTTP Method
@RequestMapping(value="/method3", method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String method3(){
return "method3";
}
// @RequestMapping default method
@RequestMapping()
@ResponseBody
public String defaultMethod(){
return "default method";
}
// @RequestMapping fallback method
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
// @RequestMapping headers
@RequestMapping(value="/method5", headers={"name=pankaj", "id=1"})
@ResponseBody
public String method5(){
return "method5";
}
// 表示将功能处理方法将生产 json 格式的数据,此时根据请求头中的 Accept 进行匹配,如请求头 Accept:application/json 时即可匹配;
@RequestMapping(value = "/produces", produces = "application/json")
@RequestMapping(produces={"text/html", "application/json"})
路由日志
该
logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=trace
在增加了上面的配置之后重启应用,便可以看到如下的日志打印:
2020-02-11 15:36:09.787 TRACE 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping :
c.d.c.UserController:
{PUT /users/{id}}: putUser(Long,User)
{GET /users/{id}}: getUser(Long)
{POST /users/}: postUser(User)
{GET /users/}: getUserList()
{DELETE /users/{id}}: deleteUser(Long)
2020-02-11 15:36:09.791 TRACE 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping :
o.s.b.a.w.s.e.BasicErrorController:
{ /error}: error(HttpServletRequest)
{ /error, produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)
2020-02-11 15:36:09.793 DEBUG 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : 7 mappings in 'requestMappingHandlerMapping'
可以看到在