Java 如何使用RequestBodyAdvice @ControllerAdvice 公共类RequestBodyAdviceChain实现RequestBodyAdvice{ @凌驾 公共布尔支持(MethodParameter、MethodParameter、类型、, 类aClass){ 返回true; } @凌驾 公共对象handleEmptyBody(对象o、HttpInputMessage、HttpInputMessage、MethodParameter、MethodParameter、, 类型类型,类aClass){ 返回o; } @凌驾 公共HttpInputMessage beforeBodyRead(HttpInputMessage HttpInputMessage,MethodParameter,MethodParameter, 类型类型,类aClass)引发IOException{ 返回httpInputMessage; } @凌驾 公共对象afterBodyRead(对象o、HttpInputMessage、HttpInputMessage、MethodParameter、MethodParameter、类型、, 类aClass){ 返回o; } }
理想情况下,流应该首先到达这些函数,然后进入控制器,但它不工作。Java 如何使用RequestBodyAdvice @ControllerAdvice 公共类RequestBodyAdviceChain实现RequestBodyAdvice{ @凌驾 公共布尔支持(MethodParameter、MethodParameter、类型、, 类aClass){ 返回true; } @凌驾 公共对象handleEmptyBody(对象o、HttpInputMessage、HttpInputMessage、MethodParameter、MethodParameter、, 类型类型,类aClass){ 返回o; } @凌驾 公共HttpInputMessage beforeBodyRead(HttpInputMessage HttpInputMessage,MethodParameter,MethodParameter, 类型类型,类aClass)引发IOException{ 返回httpInputMessage; } @凌驾 公共对象afterBodyRead(对象o、HttpInputMessage、HttpInputMessage、MethodParameter、MethodParameter、类型、, 类aClass){ 返回o; } },java,spring-mvc,spring-boot,Java,Spring Mvc,Spring Boot,理想情况下,流应该首先到达这些函数,然后进入控制器,但它不工作。@ControllerAdvice组件应该在Spring的上下文中注册为任何其他组件。确保Spring能够看到这个组件,例如,将它包含在@ComponentScan(Java配置)或(xml方式)中。注册它的首选上下文是Dispatcher Servlet的上下文。您应该直接扩展WebMvcConfigurationSupport以获得更具体的配置(而不是@EnableWebMvc,如果您采用这种方法) 然后重写此方法org.spr
@ControllerAdvice
组件应该在Spring的上下文中注册为任何其他组件。确保Spring能够看到这个组件,例如,将它包含在@ComponentScan
(Java配置)或
(xml方式)中。注册它的首选上下文是Dispatcher Servlet的上下文。您应该直接扩展WebMvcConfigurationSupport
以获得更具体的配置(而不是@EnableWebMvc
,如果您采用这种方法)
然后重写此方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#createRequestMappingHandlerAdapter
,并手动设置RequestBodyAdvice
这应该行得通你找到这个问题的解决方案了吗?我在博客上发了一篇文章,介绍了如何使用RequestBodyAdvice,并举例说明:
@ControllerAdvice
public class RequestBodyAdviceChain implements RequestBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Type type,
Class< ? extends HttpMessageConverter< ? >> aClass) {
return true;
}
@Override
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter,
Type type, Class< ? extends HttpMessageConverter< ? >> aClass) {
return o;
}
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter,
Type type, Class< ? extends HttpMessageConverter< ? >> aClass) throws IOException {
return httpInputMessage;
}
@Override
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type,
Class< ? extends HttpMessageConverter< ? >> aClass) {
return o;
}
}