任何数据,都可以使用流的方式接收,通过读取字节或字符来解析数据。
@RequestMapping("/test")
@ResponseBody
public Object test(HttpServletRequest request) {
StringBuffer sb = new StringBuffer();
try {
// 获取请求流
BufferedInputStream in = new BufferedInputStream(request.getInputStream());
int i;
char c;
while ((i = in.read()) != -1) {
c = (char) i;
sb.append(c);
}
String json = sb.toString();
Map<String, Object> map = JSON.parseObject(json, Map.class);
System.out.println(map.get("msgId"));
System.out.println(map.get("list"));
String msgId = String.valueOf(map.get("msgId"));
String result = writeData(msgId);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String writeData(String msgId) throws Exception {
Map<String, Object> result = new HashMap<>();
result.put("code", 0);
result.put("data", msgId);
String resultData = JSON.toJSONString(result);
return resultData;
}