1、Get请求获取参数
@RestController
@RequestMapping("/req")
public class ReqController {
/**
* @RequestParam用参数接收 形参默认key一样
* @RequestParam(name = "name") 或者 @RequestParam(value = "name") 与传的key一样
* @RequestParam(value = "30") 默认值30
* @RequestParam(required = false) String age age不用必传,默认为必需
* @param
* @param age
*/
@GetMapping("/get1")
public void getPath(@RequestParam(value = "name") String name1111, @RequestParam(required = false) String age){
System.out.println(name1111);
System.out.println(age);
}
/**
* 直接用参数接收,参数与key必须一样
* @param name
* @param age
*/
@GetMapping("/get2")
public void getPath2(String name,String age){
System.out.println(name);
System.out.println(age);
}
/**
* 用requset接收 request.getQueryString() 返回name=zhangsan&age=20
* @param request
*/
@GetMapping("/get3")
public void getPathForRequest(HttpServletRequest request){
System.out.println(request.getQueryString());
}
/**
* request.getParameter('参数名称')
* @param request
*/
@GetMapping("/get4")
public void getPathForRequestPara(HttpServletRequest request){
System.out.println(request.getParameter("name"));
System.out.println(request.getParameter("age"));
}
/**
* 用对象接收
* @param user
*/
@GetMapping("/get5")
public void getPathForObj(User user){
System.out.println(user.getName());
System.out.println(user.getAge());
}
/**
* Restful风格 127.0.0.1/req/get6/zhangsan
* @param name
*/
@GetMapping("/get6/{name}")
public void getForPath(@PathVariable String name){
System.out.println(name);
}
}
2、Post请求获取参数
/**
* post localhost/req/post1?name=zhangsan&age=20 params路径方式
* post body form-data
*
* @param name
* @param age
* @RequestParam
*/
@PostMapping("/post1")
public void postForForm(@RequestParam String name, @RequestParam int age) {
System.out.println(name);
System.out.println(age);
}
/**
* @RequestParam Map<String, Object> map 接收表单全部数据
* @param map
*/
@PostMapping("/post2")
public void postForForm2(@RequestParam Map<String, Object> map) {
System.out.println(map);
}
/**
* @param json
* @RequestBody 接收json数据
*/
@PostMapping("/post3")
public void postForJson(@RequestBody JSONObject json) {
System.out.println(json);
Object materialModel = json.get("title");
System.out.println(materialModel);
JSONArray periods1 = json.getJSONArray("jsonArray");
System.out.println(periods1);
List<Dict> dicts = JSONUtil.toList(periods1, Dict.class);
for (int i = 0; i < dicts.size(); i++) {
System.out.println(dicts.get(i));
}
}
/**
* 从request中获取请求体json数据
*
* @param request
* @param response
* @return
* @throws Exception
*/
@PostMapping("/post4")
public String test(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println(request.getRequestURL());
BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
sb.append(inputStr);
}
String body = sb.toString();
JSONObject json = JSONUtil.parseObj(body);
System.out.println(body);
System.out.println(json);
return "";
}