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);

}
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM