关于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; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
mybatis-plus mapper中foreach循环操作代码详解(新增或修改)
这篇文章主要介绍了mybatis-plus mapper中foreach循环操作代码详解(新增或修改),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-11-11Java异常处理UncaughtExceptionHandler使用实例代码详解
当一个线程由于未捕获异常即将终止时,Java虚拟机将使用thread . getuncaughtexceptionhandler()查询线程的uncaughtException处理程序,并调用处理程序的uncaughtException方法,将线程和异常作为参数传递2023-03-03
最新评论