My rest controller
@RestController
public class MyController {
@RequestMapping(value = "/getrawjson", method = RequestMethod.POST)
public @ResponseBody
String search(@RequestBody Map<String, Object> json, HttpServletRequest httpServletRequest) throws PushNotiException,Exception {
return "OK";
}
}
My Exception handling for invalid raw json post. I tried to use request.getInputStream() but I got the error
IllegalStateException: getInputStream() has already been called for this request
@ControllerAdvice
public class MethodArgumentNotValidExceptionHandler {
@ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public TrueIDResponse resolveException(HttpServletRequest request,Exception ex) throws IOException {
}
}
I want to keep log input raw json data on this exception.
Anyone Can help me? Thanks.
Update
As @Sean Carrol suggestion. I tried to use HttpServletRequestWrapper following a suggestion but it's still not work.
@ControllerAdvice
public class MethodArgumentNotValidExceptionHandler {
@ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public TrueIDResponse resolveException(HttpServletRequest request,Exception ex) throws IOException {
MultiReadHttpServletRequest multiReadRequest = new MultiReadHttpServletRequest((HttpServletRequest) request);
InputStream inputStream = multiReadRequest.getInputStream();
String theString = IOUtils.toString(inputStream, "UTF-8");
}
}
I got this error at the InputStream inputStream = multiReadRequest.getInputStream(); line.
java.io.IOException: Stream closed