Spring MVC获取HTTP请求的body方式
1、通过字符流或字节流获取
@ApiOperation("desc")
@RequestMapping(value = "/api", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
public RespData<Boolean> api(
HttpServletRequest request,
HttpServletResponse response) { BufferedReader br = request.getReader(); String str, body = ""; while((str = br.readLine()) != null){ body += str; } log.info(body); }
@ApiOperation("desc")
@RequestMapping(value = "/api", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
public RespData<Boolean> api(
HttpServletRequest request,
HttpServletResponse response) { int length = request.getContentLength(); ServletInputStream input = request.getInputStream(); byte[] buffer = new byte[length]; input.read(buffer, 0, length);
log.info(new String(buffer));
}
2、通过字@RequestBody获取
@ApiOperation("desc")
@RequestMapping(value = "/api", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
public RespData<Boolean> api(@ApiParam(value = "id", required = false) @RequestParam(value = "id",required = false) Long id,@RequestBody String body,
HttpServletRequest request,
HttpServletResponse response) {
log.info("body---->{}",body);
}