spring mvc HandlerInterceptorAdapter中怎样获取application/json的数据?

liu13430 发布于 2015/12/07 15:34
阅读 3K+
收藏 0
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
//解释请求数据包
String jsonString = RequestUtil.getJSONString(request);
RequestDto requestDto = RequestUtil.getRequestDto(request);
log.info("客户端请求的数据包==>\n" + jsonString);
//验证客户端签名
if (RequestUtil.verifyRequest(requestDto)){
request.setAttribute("requestDto", requestDto);
return true;
} else {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF-8");
ResponseDto responseDto = new ResponseDto();
responseDto.setHeader(ResponseHeader.getAuthError());
response.getWriter().write(JSON.toJSONString(responseDto));
return false;
}

}

public class RequestUtil{

public static String CHARSET = "UTF-8";

/**
* @详细 
* @param request
* @return 返回JSON数据包
*/
public static String getJSONString(HttpServletRequest request){
String json = "";
try {
ServletInputStream in = request.getInputStream();
String content = IOUtils.toString(in, CHARSET);
json = URLDecoder.decode(content, CHARSET);
json = json.substring(json.indexOf("=") + 1);
} catch (IOException e) {
e.printStackTrace();
}
return json;
}

}

在拦截器中获取不到JSON参数,求解?


加载中
0
liu13430
liu13430
已解决了,是自己重复读取了request.getInputStream(); 的内容导致第二次取的是空的内容,忘记了Java中输入流不可重复读,
0
圆粪
圆粪
兄弟那你是什么解决的呢
liu13430
liu13430
读一次然后保存起来,因为inputStream不支持重复读
返回顶部
顶部