1 @Target(ElementType.METHOD) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 @RequestMapping(method = RequestMethod.GET) 5 public @interface GetMapping {
GetMapping 注解已经默认封装了@RequestMapping(method = RequestMethod.GET)
所以,比前文 使用 @RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET) 更方便
1 mport org.springframework.web.bind.annotation.*; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 @RestController 7 public class GetController { 8 9 private Map<String,Object> map = new HashMap<>(); 10 11 12 13 @GetMapping("/v2/request") 14 public Object testPage1(int from,int size){ 15 map.clear(); 16 map.put("from",from); 17 map.put("size",size); 18 return map; 19 } 20 21 @GetMapping("/v2/request_page") 22 public Object testPage2(@RequestParam(defaultValue = "0",name = "page") int from,int size){ 23 map.clear(); 24 map.put("from",from); 25 map.put("size",size); 26 return map; 27 } 28 }
可以使用@RequestParam添加默认值,设置别名。
二 获取请求头信息
1 @GetMapping("/v2/get_header") 2 public Object getHeader(@RequestHeader("mytoken") String mytoken ,String id){ 3 map.clear(); 4 map.put("token",mytoken); 5 map.put("id",id); 6 return map; 7 8 }
三 使用 HttpServletRequest 获取请求信息
1 @GetMapping("/v2/get_servlet") 2 public Object testServlet(HttpServletRequest request){ 3 4 map.clear(); 5 map.put("id",request.getParameter("id")); 6 return map; 7 8 }