HttpServletRequest获取POST请求Body参数3种方法
·
- 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(); }
2、@RequestBody
@RequestMapping(value = "hello", method = {RequestMethod.POST}) @ResponseBody public String batchDisabledUsers(@RequestBody xxxDTO xx) { }
3、@RequestParam
@RequestMapping(value = "/testurl", method = RequestMethod.POST) @ResponseBody public StringTestUrl(@RequestParam("username")String username, @RequestParam("pwd")String pwd) { String txt = username + pwd; return txt; }
更多推荐
已为社区贡献2条内容
所有评论(0)