get与post两种方式的区别:对于本次主题而言,最显著的区别就是get请求方式参数是在url后,而post请求方式的参数是在request body中。因此两者获取参数的方式也大不一样。
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty(value = "id")
private Integer id;
@JsonProperty(value = "name")
private String name;
@JsonProperty(value = "age")
private Integer age;
@JsonProperty(value = "hobby")
private List hobby;
}
-
@GetMapping("/get")
public User getUserById(Integer id) {
if (id.intValue() == 0) {
return null;
}
return list.get(id);
}
访问 http://ip:port/xx/get?id=1
@GetMapping("/get")
public User getUserById(@RequestParam(name = "id", required = true) Integer id) {
if (id.intValue() == 0) {
return null;
}
return list.get(id);
}
其中RequestParam中name表示url中请求的字段名,当required为true时,表示该参数必填;defaultValue表示当该参数没有传递数据时给出的默认值,如defaultvalue=”0”
访问方式同上
@GetMapping("/get/{id}")
public User getUserByPathValue(@PathVariable(name = "id", required = true) Integer id) {
return list.get(id);
}
该方式用来获取路径中的参数。@PathVariable中的字段含义同RequestParam
访问http:///ip:port/xx/get/1 ——->对应于get/{id}
@GetMapping("/get")
public User getUserById(HttpServletRequest request) {
Integer id = Integer.parseInt(request.getParameter("id"));
if (id.intValue() == 0) {
return null;
}
return list.get(id);
}
访问 http://ip:port/xx/get?id=1
一般而言,post形式的参数被放在请求体中以application/json的形式被后端获取
content-type:application/json
@PostMapping("/save")
public User saveUser(@RequestBody User user) {
list.add(user);
return user;
}
会把这些字段组装到对象中
map中存放的键值对就对应于json中的键值对 content-type:application/json
@PostMapping("/save")
public User saveUser(@RequestBody Map map) {
Integer id = (Integer) map.get("id");
String name = (String) map.get("name");
Integer age = (Integer) map.get("age");
List hobby=(List) map.get("hobby");
User user = new User(id, name, age, hobby);
list.add(user);
return user;
}
将content-type改为x-www-form-urlencoded
@PostMapping("/save")
public User save(HttpServletRequest request) {
Integer id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
String parameter = request.getParameter("hobby");
User user = new User(id, name, age, null);
list.add(user);
return user;
}
content-type:application/json
@PostMapping("/save")
public User save(HttpServletRequest request) throws IOException, JSONException {
JSONObject jsonObject = handlerData(request);
Integer id = jsonObject.getInteger("id");
String name = jsonObject.getString("name");
Integer age = jsonObject.getInteger("age");
List hobby = jsonObject.getObject("hobby", List.class);
User user = new User(id, name, age, hobby);
list.add(user);
return user;
}
//这里使用的是alibaba的json工具类
public static JSONObject handlerData(HttpServletRequest request) throws IOException, JSONException {
StringBuffer sb = new StringBuffer();
InputStream is = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s);
}
if (sb.toString().length() <= 0) {
return null;
} else {
return JSONObject.parseObject(sb.toString());
}
}
本来使用的是org.json包下的JSONObject,无奈启动就报 org.json.JSONException,后改为使用alibaba的fastjson
注意:使用缓冲输入流读取的是这里的json,因此是一行一行读取的:
!
读取的时候 报出异常:not close json text,token:: 将json改为如下所示