Spring MVC get body of the HTTP request

 

1, obtained by the flow stream, or byte character

@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, through the word @RequestBody Get
@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);

}
 
 

Guess you like

Origin www.cnblogs.com/xuzhujack/p/12387542.html