WebAPI服务器收发json数据
1. 服务端接收
1.1 通过HttpServletRequest request获取json信息
@RequestMapping("/dice/init")
@ResponseBody
public String diceInit(HttpServletRequest request, Integer Count) {
String param= null;
try {
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(),
"UTF-8"));//通过I0流获取data body
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null){
responseStrBuilder.append(inputStr);
}
JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
param= jsonObject.toJSONString();
System.out.println(param);
} catch (Exception e) {
e.printStackTrace();
}
}
1.2 使用@RequestBody方法获取
@RequestMapping("/dice/valueInit")
@ResponseBody
public BaseResponse valueInit(Integer Round, Integer Count, @RequestBody JSONObject obj){
String userName = obj.getString("userName");
String steamerName = obj.getString("steamerName");
String diceUUID = safeUtils.shortMD5(userName+"&"+steamerName+ textUtils.getCurrentTime());
}
2. 服务端返回
2.1 直接返回JSONObject
2.2 返回实体类