讲解springboot接口,http的get请求,各个注解使用
1、单一参数@RequestMapping(path = "/{id}", method = RequestMethod.GET)
1) public String getUser(@PathVariable String id ) {}
2)@RequestMapping(path = "/{depid}/{userid}", method = RequestMethod.GET) 可以同时指定多个提交方法
getUser(@PathVariable("depid") String departmentID,@PathVariable("userid") String userid)
3)一个顶俩
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
4)@RequestParam(value = "name", required = true)
可以设置默认值,比如分页
5)@RequestBody 请求体映射实体类
需要指定http头为 content-type为application/json charset=utf-8
6)@RequestHeader 请求头,比如鉴权
@RequestHeader("access_token") String accessToken
7)HttpServletRequest request自动注入获取参数
代码:
package net.xdclass.demo.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.xdclass.demo.domain.TestUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
//@RestController返回json格式
@RestController
//定义控制器对应法URL
@RequestMapping("/testGet")
public class GetController {
private Map params = new HashMap<>();
//++++++++++++++++++++
/***
* 第一类:GET请求路径参数
* 1、@PathVariable
* 获取路径参数。即url/{id}这种形式。
*
* 2、@RequestParam
* 获取查询参数。即url?name=这种形式
* @param id
* @param name
*/
@GetMapping("/demo/{id}")
public Object demo(@PathVariable(name = "id") String id,
@RequestParam(name = "name") String name,
@RequestParam(name = "age") int age) {
params.clear();
params.put("id",id);
params.put("name",name);
params.put("age",age);
return params;
}
//++++++++++++++++++++
/**
* 通过request获取参数【重点】
* HttpServletRequest request
* @param request
* @return
*/
@GetMapping("test_request")
public Object testRequest(HttpServletRequest request){
params.clear();
String id = request.getParameter("id");
params.put("id", id);
String name = request.getParameter("name");
params.put("name",name);
return params;
}
/**
* 功能描述:测试restful协议,从路径中获取字段
* 定义接口协议:小写字母 + 下划线,防止有些语言不支持驼峰
* 或者这样:但是赋值变量名必须与参数一致
* path = "/{city_id}/{user_id}"
* findUser(@PathVariable String city_id,
* @PathVariable String user_id )
* @param cityId
* @param userId
* @return
*/
@RequestMapping(path = "/{city_id}/{user_id}", method = RequestMethod.GET)
public Object findUser(@PathVariable("city_id") String cityId,
@PathVariable("user_id") String userId ){
params.clear();
params.put("cityId", cityId);
params.put("userId", userId);
return params;
}
/**
* 功能描述:测试GetMapping
* @param from
* @param size
* @return
*/
@GetMapping(value="page_user1")
public Object pageUser(int from, int size ){
params.clear();
params.put("from", from);
params.put("size", size);
return params;
}
/**
* 功能描述:默认值,是否必须的参数
* /testGet/page_user2?size=10&page=1
* @param from
* @param size
* @return
* @RequestParam 传递默认值 ,required=true为必须参数
* (@RequestParam(defaultValue="0",name="page",required = true)
*/
@GetMapping(value="page_user2")
public Object pageUserV2(@RequestParam(defaultValue="0",name="page") int from, int size ){
params.clear();
params.put("from", from);
params.put("size", size);
return params;
}
/**
* 功能描述:bean对象传参
* 注意:1、注意需要指定http头为 content-type为application/json
* 2、使用body传输数据
* @param user
* @return
*/
@RequestMapping("save_user")
public Object saveUser(@RequestBody TestUser user){
params.clear();
params.put("user", user);
return params;
}
/**
* 功能描述:测试获取http头信息
* @param accessToken
* @param id
* @return
*/
@GetMapping("get_header")
public Object getHeader(@RequestHeader("access_token") String accessToken, String id){
params.clear();
params.put("access_token", accessToken);
params.put("id", id);
return params;
}
}