SpringMVC 中 restful 风格请求与普通请求

普通请求:

  • @RequestMapping 注解中 value属性 指定请求路径, params属性 指定请求参数名,method属性 指定请求类型
  • @RequestParam 注解来映射请求参数
@RequestMapping(value = "/index", method = RequestMethod.GET, params = {
    
    "name","id"})
public String index(@RequestParam("name") String name,@RequestParam("id") int id){
    
    
    System.out.println("执行了 index 方法...");
    System.out.println("name:  "+name+"    id:   "+id);
    return "index";
}

restful 风格请求:

  • @RequestMapping 注解中 value属性 指定请求路径指定路径,路径中包含请求参数,请求参数用{} 包裹,params属性 指定请求参数名。
  • @PathVariable 注解来映射请求参数名。
@RequestMapping(value = "/restful/{name}/{id}",method = RequestMethod.GET)
public String indexRest(@PathVariable("name") String name,@PathVariable("id") int id){
    
    
    System.out.println("执行了 indexRest 方法...");
    System.out.println("name:  "+name+"    id:   "+id);
    return "index";
}

猜你喜欢

转载自blog.csdn.net/Start1234567/article/details/112393347