关于HttpServletRequest获取POST请求Body参数的3种方式
目录
- HttpServletRequest获取POST请求Body参数方式
- 第一种方式
- 第二种方式
- 第三种方式
- 总结
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 jajavascriptvax.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.appejavascriptnd(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) www.devze.com@ResponseBody public StringpythonTestUrl(@RequestParam("username")String username,javascript @RequestParam("pwd")String pwd) { String txt = username + pwd; return txt; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论