SpringBoot-2.X 學習筆記02 獲取請求參數
1 @PathVariable 請求請求方式爲:/v1/{id}/{name}&{pwd}
2 @RequestParam(defaultValue = “123456”,required = true,name = “id”)String id
3 @RequestBody Student student 請求請求方式爲:content-type 必須爲 applocation/json
4 @RequestHeader(“access_token”) String token 請求請求方式爲:head
package com.xu.springboot.controler;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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;
import com.xu.springboot.entity.Student;
/**
* SpringBoot-2.* 獲取請求參數
* @ClassName: HttpController
* @Description: TODO
* @author: hyacinth
* @date: 2019年8月1日 下午10:02:12
* @Copyright: hyacinth
*/
@RestController
@RequestMapping("/request")
public class RequestController {
/**
* 1 從請求URL中獲取請求參數
* @Title: test001
* @Description: TODO
* @param: id
* @param: name
* @param: pwd
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:09:30
*/
@RequestMapping(path = "/v1/{id}/{name}&{pwd}",method = RequestMethod.GET)
public Object test001(@PathVariable("id")String id,@PathVariable("name")String name,@PathVariable("pwd")String pwd) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", id);
map.put("name", name);
map.put("pwd", pwd);
return map;
}
/**
* 2 從請求URL中獲取請求參數
* @Title: test002
* @Description: TODO
* @param: a
* @param: b
* @return: Object Json 數據
* @date: 2019年7月22日 下午11:14:20
*/
@GetMapping("/v2")
public Object test002(String id,String name,String pwd) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", id);
map.put("name", name);
map.put("pwd", pwd);
return map;
}
/**
* 3 從請求URL中獲取請求參數
* @Title: test003
* @Description: TODO
* @param: id
* @param: pwd
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:14:19
*/
@GetMapping("/v3")
public Object test003(@RequestParam(defaultValue = "0000",required = true,name = "id")String id,@RequestParam(defaultValue = "0000",required = true,name = "pwd")String pwd) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", id);
map.put("pwd", pwd);
return map;
}
/**
* 4 從請求RequestBody中獲取請求參數 content-type 必須爲 applocation/json
* @Title: test004
* @Description: TODO
* @param: student
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:15:02
*/
@RequestMapping("/v4")
public Object test004(@RequestBody Student student) {
Map<String, Student> map=new HashMap<String, Student>();
map.put("Student", student);
return map;
}
/**
* 5 從請求RequestHeader中獲取請求參數
* @Title: test005
* @Description: TODO
* @param: token
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:18:08
*/
@GetMapping("/v5")
public Object test005(@RequestHeader("access_token") String token) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("Student", token);
return map;
}
/**
* 6 從請求HttpServletRequest中獲取請求參數
* @Title: test006
* @Description: TODO
* @param: @param request
* @param: @return
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:21:06
* @throws
*/
@GetMapping("/v6")
public Object test006(HttpServletRequest request) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("Student", request.getParameter("access_token"));
return map;
}
/**
* 7 POST 請求
* @Title: test007
* @Description: TODO
* @param: id
* @param: pwd
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:23:42
*/
@PostMapping("/post")
public Object test007(String id,String pwd) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", id);
map.put("pwd", pwd);
return map;
}
/**
* 8 PUT請求
* @Title: test008
* @Description: TODO
* @param: id
* @param: pwd
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:24:34
*/
@PutMapping("/put")
public Object test008(String id,String pwd) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", id);
map.put("pwd", pwd);
return map;
}
/**
* 9 DELETE請求
* @Title: test009
* @Description: TODO
* @param: id
* @param: pwd
* @return: Object Json 數據
* @date: 2019年8月1日 下午10:24:38
*/
@DeleteMapping("/delete")
public Object test009(String id,String pwd) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", id);
map.put("pwd", pwd);
return map;
}
}