1.Get请求
1)测试restful协议,从路径中获取字段
单一参数@RequestMapping(path = "/{id}", method = RequestMethod.GET)
public String getUser(@PathVariable String id ) {}
2)测试@GetMapping =@RequestMapping(Method={RequestMethod.GET})
@PostMapping =@RequestMapping(Method={RequestMethod.POST})
@PutMapping =@RequestMapping(Method={RequestMethod.PUT})
@DeleteMapping=@RequestMapping(Method={RequestMethod.DELETE})
3)@RequestParam(value = "name", required = true)
可以设置默认值,比如分页
4)@RequestBody 请求体映射实体类
需要指定http头为 content-type为application/json charset=utf-8
5)@RequestHeader 请求头,比如鉴权
@RequestHeader("access_token") String accessToken
6)HttpServletRequest request自动注入获取参数
package com.example.demo.controller;import com.example.demo.pojo.User;import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;import java.util.HashMap;import java.util.Map;/*** @author Suncm* @ClassName GetController* @Date /5/15 17:20* @Version 1.0*///测试http协议的get请求以及注解的使用@RestControllerpublic class GetController {private Map<String, Object> params = new HashMap<String,Object>();/***@Author Suncm*@Despription //测试restful协议,从路径中获取字段*@Date 10:31 /5/16*@Param [cityId, userId]*@return java.lang.Object**/@RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET)public Object findUser(@PathVariable("city_id") String cityId, @PathVariable(value = "user_id",required = true) String userId){params.clear();params.put("cityId",cityId);params.put("userId",userId);for (Map.Entry<String,Object> entry:params.entrySet()) {System.out.println(entry.getKey()+",value="+entry.getValue());}return params;}/**@Author Suncm*@Despription //测试@getMapping*@Date 11:02 /5/16*@Param [form, size]*@return java.lang.Object**/@GetMapping(value = "/v1/page_user1")public Object pageUser(int from,int size){params.clear();params.put("from",from);params.put("size",size);return params;}/**@Author Suncm*@Despription //测试参数默认值*@Date 11:13 /5/16*@Param [from, size]*@return java.lang.Object**/@GetMapping(value = "/v1/page_user2")public Object pageUser2(@RequestParam(defaultValue = "0",name = "page") int from,int size){params.clear();params.put("from",from);params.put("size",size);return params;}/**@Author Suncm*@Despription //bean 对象传参* 1、注意需要指定http头为 content-type为application/json* 2、使用body传输数据*@Date 11:27 /5/16*@Param [user]*@return java.lang.Object**/@RequestMapping(value = "/v1/save_user")public Object saveUser(@RequestBody User user){params.clear();params.put("user",user);return params;}/**@Author Suncm*@Despription //获取请求头*@Date 11:47 /5/16*@Param []*@return java.lang.Object**/@RequestMapping("/v1/get_header")public Object getHeader(@RequestHeader("access_token")String accessToken,String id){params.clear();params.put("accessToken",accessToken);params.put("id",id);return params;}/**@Author Suncm*@Despription //参数存储到request里*@Date 11:59 /5/16*@Param [request]*@return java.lang.Object**/@GetMapping("/v1/test_request")public Object testRequest(HttpServletRequest request){params.clear();String id = request.getParameter("id");params.put("id", id);return params;}/**@Author Suncm*@Despription //测试@PostMapping*@Date 14:41 /5/16*@Param [id, pwd]*@return java.lang.Object**/@PostMapping("/v1/test_post")public Object postTest(String id,String pwd){params.clear();params.put("id",id);params.put("pwd",pwd);return params;}/**@Author Suncm*@Despription //测试@PutMapping*@Date 14:44 /5/16*@Param [id]*@return java.lang.Object**/@PutMapping("/v1/test_put")public Object changeUser(String id){params.clear();params.put("id",id);return params;}@DeleteMapping("/v1/test_del")public Object delUser(String id){params.clear();params.put("id",id);return params;}}