> 文档中心 > SpringBoot中的Rest映射(即如何使用form表单进行PUT和DELETE方法的提交),常用的几种参数注入注解(详细介绍)

SpringBoot中的Rest映射(即如何使用form表单进行PUT和DELETE方法的提交),常用的几种参数注入注解(详细介绍)


一:SpringBoot中的Rest映射

1.Rest风格介绍
首先解释什么是Rest风格

REST风格REST 是一种软件架构风格,其本身是围绕HTTP的URI资源进行约束。URI 中不能有动词,因为 URI 本身是资源的表示,所以网址中不能有动词,只能由名词,动词由 HTTP 协议中的操作:GET、POSE、PUT、DELETE 四种方法表示。如果有自定以的状态码,由于 REST 本身是根据 HTTP 协议进行规范的,所以尽量使用 HTTP 协议的状态码。200:OK 请求响应成功,服务器返回数据,该操作是幂等的201:CREATED 新建或者修改数据成功204:NOT CONTENT 删除数据成功400:BAD REQUEST 用户发出的请求有问题,该操作是幂等的401Unauthoried 表示用户没有认证,无法进行操作403Forbidden 用户访问是被禁止的404Not Found 请求资源不存在422Unprocesable Entity 当创建一个对象时,发生一个验证错误500:INTERNAL SERVER ERROR 服务器内部错误,用户无法判断请求是否成功503Service Unavailable 服务器不可用状态,多半是服务器问题,例如CPU占用率打等。风格差别:

Rest风格样例:

@RestControllerpublic class TestController { @GetMapping("/user")    public String getUser(){ return null;    } @PostMapping("/user")    public String postUser(){ return null;    } @PutMapping("/user")    public String putUser(){ return null;    } @DeleteMapping("/user")    public String deleteUser(){ return null;    }}

SpringBoot中的Rest映射(即如何使用form表单进行PUT和DELETE方法的提交),常用的几种参数注入注解(详细介绍)

2.如何在form表单中使用put和delete方法提交
错`误案列:

@RestControllerpublic class TestController {    @GetMapping("/user")    public String getUser(){ return "GET";    }    @PostMapping("/user")    public String postUser(){ return "POST";    }    @PutMapping("/user")    public String putUser(){ return "PUT";    }    @DeleteMapping("/user")    public String deleteUser(){ return "DELETE";    }}

html

测试REST风格<form action="/user" method="GET">    <input type="submit" value="REST-GET"></form><form action="/user" method="POST">    <input type="submit" value="REST-POST"></form><form action="/user" method="PUT">    <input type="submit" value="REST-PUT"></form><form action="/user" method="DELETE">    <input type="submit" value="REST-DELETE"></form>

如果使用以上代码进行form表单的访问的话,会出现请求的PUT和DELETE,都会走get接口,
查看源码
在这里插入图片描述
在yaml文件中开启

spring:  mvc:    hiddenmethod:      filter: enabled: true

在这里插入图片描述
由于springboot的HiddenHttpMethodFilter里面配置了,所以可以使用以下方法
请注意:使用PUT或者DELETE方法进行提交,form表单中method必须等于post

测试REST风格<form action="/user" method="GET">    <input type="submit" value="REST-GET"></form><form action="/user" method="POST">    <input type="submit" value="REST-POST"></form><form action="/user" method="POST">    <input  name="_method" type="hidden" value="PUT">    <input type="submit" value="REST-PUT"></form><form action="/user" method="POST">    <input  name="_method" type="hidden" value="DELETE">    <input type="submit" value="REST-DELETE"></form>

二:SpringBoot中常用的几种参数注解

1.注解:@PathVariable、@RequestHeader、@ModelAttribute
controller层

@GetMapping("/car/{id}/owner/{username}")    public Map<String,Object> getCar(@PathVariable("id") Integer id,  @PathVariable("username") String username,  @PathVariable Map<String,Object> pv,  @RequestHeader("User-Agent") String userAgent,  @RequestHeader Map<String,String> header){ Map<String, Object> map = new HashMap<>(); map.put("id",id); map.put("username",username); map.put("pv",pv); map.put("userAgent",userAgent); map.put("header",header); return map;    }    @GetMapping("/car/{id}")    public Map<String,Object> getCarParm(@PathVariable("id") Integer id,      @RequestParam("age") Integer age,      @RequestParam("interest") List<String> interest,      @RequestParam Map<String,Object> params){//      @CookieValue("_ga") String _ga,//      @CookieValue("_ga") Cookie cookie){ Map<String, Object> map = new HashMap<>(); map.put("id",id); map.put("age",age); map.put("interest",interest); map.put("params",params);// map.put("_ga",_ga);// map.put("cookie",cookie); return map;    }
<a href="/car/1/owner/wang">    @PathVariable、</a><a href="/car/1/owner/wang">    @RequestHeader、</a><a href="/car/1?age=18&interest=girl&interest=popise">    @ModelAttribute、</a>

2.使用原生的mvc中的HttpServletRequest request

 @GetMapping("/goto")    public String goToPage(HttpServletRequest request){ request.setAttribute("msg","成功了。。。。"); request.setAttribute("code",200); return "forward:/success";    }    @ResponseBody    @GetMapping("/success")    public Map<String, Object> success(@RequestAttribute("code") Integer code,     @RequestAttribute("msg") String msg,     HttpServletRequest request){ Map<String, Object> map = new HashMap<>(); map.put("msg",msg); map.put("code",code); Object msg1 = request.getAttribute("msg"); if(msg.equals(msg1)){     map.put("status",true); }// map.put("request",request); return map;    }

3.使用@RequestBody注入

<form action="/save" method="post">    测试@RequestBody获取数据    用户名:<input name="userName" />    邮箱:<input name="email"/>    <input type="submit" value="提交"/></form>
@PostMapping("/save")    @ResponseBody    public Map<String, Object> save(@RequestBody String content){ Map<String, Object> map = new HashMap<>(); map.put("STring",content); return map;    }