赞
踩
除了@RequestBody方式外,我们还可以通过HttpServletRequest方式获取参数。
比如参数是:
{
"code": "xxx"
}
获取方式:
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; private JSONObject getJsonRequest(HttpServletRequest request) { JSONObject result = null; StringBuilder sb = new StringBuilder(); try (BufferedReader reader = request.getReader();) { char[] buff = new char[1024]; int len; while ((len = reader.read(buff)) != -1) { sb.append(buff, 0, len); } result = JSONUtil.parseObj(sb.toString()); } catch (IOException e) { log.error("", e); } return result; }
sb.toString()就是上面json字符串,转成JSONObject对象后就可以拿到:
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Result login(HttpServletRequest request) {
JSONObject param = this.getJsonRequest(request);
String code = param.getStr("code");
// ...
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。