关于HttpServletRequest获取POST请求Body参数的3种方式
更新时间:2023年11月18日 15:10:02 作者:蛋焊工
这篇文章主要介绍了关于HttpServletRequest获取POST请求Body参数的3种方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
HttpServletRequest获取POST请求Body参数方式
第一种方式
request.getInputStream()
/**
* If the parameter data was sent in the request body, such as occurs
* with an HTTP POST request, then reading the body directly via
* @see javax.servlet.ServletRequest#getInputStream or
* @see javax.servlet.ServletRequest#getReader
* @param request HttpServletRequest
* @return String
*/
public static String getPostData(HttpServletRequest request) {
StringBuilder data = new StringBuilder();
String line;
BufferedReader reader;
try {
reader = request.getReader();
while (null != (line = reader.readLine())) {
data.append(line);
}
} catch (IOException e) {
return null;
}
return data.toString();
}第二种方式
@RequestBody
@RequestMapping(value = "hello", method = {RequestMethod.POST})
@ResponseBody
public String batchDisabledUsers(@RequestBody xxxDTO xx) {
}第三种方式
@RequestParam
@RequestMapping(value = "/testurl", method = RequestMethod.POST)
@ResponseBody
public StringTestUrl(@RequestParam("username")String username,
@RequestParam("pwd")String pwd) {
String txt = username + pwd;
return txt;
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
java集合——Java中的equals和hashCode方法详解
本篇文章详细介绍了Java中的equals和hashCode方法详解,Object 类是所有类的父类,非常具有实用价值,需要的朋友可以参考下。2016-10-10
SpringBoot统一返回处理出现cannot be cast to java.lang.String异常解决
这篇文章主要给大家介绍了关于SpringBoot统一返回处理出现cannot be cast to java.lang.String异常解决的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下2023-09-09
elasticsearch开发中data-streams使用解析
这篇文章主要为大家介绍了elasticsearch开发中data-streams使用解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-08-08


最新评论