- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的休息 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";
}
}
我对无效原始 json 帖子的异常处理。我尝试使用 request.getInputStream() 但收到错误
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 {
}
}
我想保留此异常的日志输入原始 json 数据。
有人可以帮助我吗?谢谢。
更新
正如@Sean Carrol 的建议。我按照建议尝试使用 HttpServletRequestWrapper 但仍然不起作用。
@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");
}
}
我在 InputStream inputStream = multiReadRequest.getInputStream(); 处收到此错误线。
java.io.IOException: Stream closed
最佳答案
我可以解决这个问题,只需添加一个过滤器,拦截当前的 HttpServletRequest 并将其包装在自定义的 HttpServletRequestWrapper (MultipleReadHttpRequest)
@Component
public class CachingRequestBodyFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest currentRequest = (HttpServletRequest) servletRequest;
MultipleReadHttpRequest wrappedRequest = new MultipleReadHttpRequest(currentRequest);
chain.doFilter(wrappedRequest, servletResponse);
}
}
servlet 3.0 的 MultipleReadHttpRequest 类
public class MultiReadHttpServletRequest extends HttpServletRequestWrapper {
private ByteArrayOutputStream cachedBytes;
public MultiReadHttpServletRequest(HttpServletRequest request) {
super(request);
}
@Override
public ServletInputStream getInputStream() throws IOException {
if (cachedBytes == null)
cacheInputStream();
return new CachedServletInputStream();
}
@Override
public BufferedReader getReader() throws IOException{
return new BufferedReader(new InputStreamReader(getInputStream()));
}
private void cacheInputStream() throws IOException {
/* Cache the inputstream in order to read it multiple times. For
* convenience, I use apache.commons IOUtils
*/
cachedBytes = new ByteArrayOutputStream();
IOUtils.copy(super.getInputStream(), cachedBytes);
}
/* An inputstream which reads the cached request body */
public class CachedServletInputStream extends ServletInputStream {
private ByteArrayInputStream input;
public CachedServletInputStream() {
/* create a new input stream from the cached request body */
input = new ByteArrayInputStream(cachedBytes.toByteArray());
}
@Override
public int read() throws IOException {
return input.read();
}
@Override
public boolean isFinished() {
return input.available() == 0;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener listener) {
throw new RuntimeException("Not implemented");
}
}
}
关于spring - @ExceptionHandler 如何从 HttpServletRequest 获取原始 json post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605079/
我知道 Exception 是所有异常的父级,但我认为当您为特定异常类设置 @ExceptionHandler 时,它应该处理该特定异常。 也许您可以指出我在以下代码中遗漏的内容,以便 MethodA
我开发了一个 Web 应用程序,其中表单验证异常应该由 @ExceptionHandler 处理(需要灵活),一般系统异常由 SimpleMappingExceptionResolver 处理(执行电
在我的应用程序中,我在 web.xml 中配置了一个错误页面。我需要根据条件覆盖特定 Controller 。在这里,当条件成立时,我需要重定向到特定的错误页面,否则应该呈现正常的错误页面。这是代码。
经过一段时间的研究和尝试不同的事情后,我仍然无法在 jUnit 集成测试中调用我的 @ExceptionHandler。请帮我理解为什么? @RequestMapping(value = "/some
我将在我的 Web 应用程序上引入全局处理程序: @ControllerAdvice public class GlobalControllerExceptionHandler { @Exce
我一直在兜圈子试图修复测试,但 SO 或其他在线资源都没有提供解决方案。我有这个@ControllerAdvice处理 MyException 的方法异常(exception)是: @Controll
这个问题在 Google 领域已被问过几次,但我似乎无法将这些解决方案应用于我的情况。我的 J2EE 应用程序使用 Spring,之前我使用 SimpleMappingExceptionResolve
我正在使用 springmvc 为客户端创建 restful api,我有一个用于检查 accesstoken 的拦截器。 public class AccessTokenInterceptor ex
我的 Controller 中有一个方法可以处理应用程序抛出的异常。所以我有一个这样的方法。 @Controller public class ExceptionController { @R
我使用 @ControllerAdvice 来处理我所有的应用程序异常: @ControllerAdvice public class ExceptionHandlingController {
我正在使用以下代码处理我的受控异常: @ExceptionHandler(MyException.class) @ResponseStatus(HttpStatus.NOT_FOUN
我构建了一个简单的 ExceptionHandler 类: class ExceptionHandler{ public function __construct(){ set
我不知道如何通过@ExceptionHandler 处理一种以上的异常。 我需要以编程方式处理这些异常,为此我需要一个共享引用。这是通过此引用“Exception ex”完成的吗?我不这么认为,因为没
我正在使用默认 AnnotationMethodHandlerAdapter,我认为它应该启用对@ExceptionHandler 的支持。不幸的是,如果调用如下所示的处理程序方法,则会抛出 Serv
我正在使用 spring 的 @ControllerAdvice 和 @ExceptionHandler 进行异常处理。任何方法都会从 Controller 中抛出自定义异常,并由相应的 @Excep
我有一个像 @RestController public class CustomerRestController { @Autowired private CustomerManag
在我的Micronaut应用程序中,我定义了ExceptionHandler,应该捕获WorkflowException并返回状态码为412且主体为HttpResponse的WorkflowExcep
我有这样的异常处理程序: @ControllerAdvice public classMyExceptionHandler extends ResponseEntityExceptionHandler
我在 spring RestController 中有这样的异常处理程序。 @ExceptionHandler(AuthException.class) public ResponseEntit
我遇到问题,然后我 try catch 异常。 这是我的类,它实现了 ResponseErrorHandler: public class ErrorGenerator implements
我是一名优秀的程序员,十分优秀!