关于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; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
浅谈SpringBoot中的@Conditional注解的使用
这篇文章主要介绍了浅谈SpringBoot中的@Conditional注解的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-04-04springboot CommandLineRunner接口实现自动任务加载功能
这篇文章主要介绍了springboot CommandLineRunner接口实现自动任务加载功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-05-05关于springboot集成swagger及knife4j的增强问题
这篇文章主要介绍了springboot集成swagger以及knife4j的增强,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-03-03
最新评论