Loading

HttpServletRequest 获取 body 内容

获取 HTTP 字符串 body

String getBodytxt(HttpServletRequest request) {
    BufferedReader br = request.getReader();
    String str, wholeStr = "";
    while((str = br.readLine()) != null){
        wholeStr += str;
    }
    return wholeStr;
}

获取 HTTP 二进制 body

private String getBodyData(HttpServletRequest request) {
    StringBuffer data = new StringBuffer();
    String line = null;
    BufferedReader reader = null;
    try {
        reader = request.getReader();
        while (null != (line = reader.readLine()))
            data.append(line);
    } catch (IOException e) {
    } finally {
    }
    return data.toString();
}

@RequestBody 获取 body

@RequestBody 可以使用 JSONObject、Map、ObjectDTO 等绑定 body。

@RequestMapping(value = "/testurl", method = RequestMethod.POST)
@ResponseBody
public ServiceResult TestUrl(HttpServletRequest request, @RequestBody JSONObject jsonObject){
    String username =  jsonObject.get("username").toString();
    String pwd = jsonObject.get("pwd").toString();
}

@RequestParam 获取 body

@RequestMapping(value = "/testurl", method = RequestMethod.POST)
@ResponseBody
public ServiceResult TestUrl(HttpServletRequest request, @RequestParam("username") String username, @RequestParam("pwd") String pwd)  {
    String txt = username + pwd;
}

转载自:http://www.51gjie.com/javaweb/963.html

posted @ 2021-06-22 18:39  LB477  阅读(7037)  评论(0)    收藏  举报