首页 > 编程语言 > 详细

Spring MVC获取HTTP请求的body方式

时间:2020-03-01 09:07:47      阅读:405      评论:0      收藏:0      [点我收藏+]

 

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

}
 
 

Spring MVC获取HTTP请求的body方式

原文:https://www.cnblogs.com/xuzhujack/p/12387542.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!