springboot基础之GetMapping
1 @Target(ElementType.METHOD)
2 @Retention(RetentionPolicy.RUNTIME)
spring framework runtime3 @Documented
4 @RequestMapping(method = RequestMethod.GET)
5public @interface GetMapping {
GetMapping 注解已经默认封装了@RequestMapping(method = RequestMethod.GET)
所以,⽐前⽂使⽤ @RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET) 更⽅便 1 mport org.springframework.web.bind.annotation.*;
2
3import java.util.HashMap;
4import java.util.Map;
5
6 @RestController
7public class GetController {
8
9private Map<String,Object> map = new HashMap<>();
10
11
12
13 @GetMapping("/v2/request")
14public Object testPage1(int from,int size){
15 map.clear();
16 map.put("from",from);
17 map.put("size",size);
18return map;
19 }
20
21 @GetMapping("/v2/request_page")
22public Object testPage2(@RequestParam(defaultValue = "0",name = "page") int from,int size){
23 map.clear();
24 map.put("from",from);
25 map.put("size",size);
26return map;
27 }
28 }
可以使⽤@RequestParam添加默认值,设置别名。
⼆获取请求头信息
1 @GetMapping("/v2/get_header")
2public Object getHeader(@RequestHeader("mytoken") String mytoken ,String id){
3 map.clear();
4 map.put("token",mytoken);
5 map.put("id",id);
6return map;
7
8 }
三使⽤ HttpServletRequest 获取请求信息
1 @GetMapping("/v2/get_servlet")
2public Object testServlet(HttpServletRequest request){
3
4 map.clear();
5 map.put("id",Parameter("id"));
6return map;
7
8 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论