【问题标题】:Spring RequestBodyAdvice not being triggeredSpring RequestBodyAdvice 未被触发
【发布时间】:2017-01-17 08:51:38
【问题描述】:

我声明了一个实现RequestBodyAdvice@ControllerAdvice。我的问题是它没有被触发。我在同一个包中有一个ResponseBodyAdvice,它按预期工作。

@RestControllerAdvice
public class RestPreProcessingAdvice implements RequestBodyAdvice {

  @Override
  public boolean supports(final MethodParameter methodParameter, final Type targetType,
      final Class<? extends HttpMessageConverter<?>> converterType) {
    return checkIfElegiable(...);
  }

  @Override
  public Object handleEmptyBody(final Object body, final HttpInputMessage inputMessage, final MethodParameter parameter,
      final Type targetType, final Class<? extends HttpMessageConverter<?>> converterType) {
    return body;
  }

  @Override
  public HttpInputMessage beforeBodyRead(final HttpInputMessage inputMessage, final MethodParameter parameter,
      final Type targetType, final Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
    return doSomeProcessing(...);
  }

  @Override
  public Object afterBodyRead(final Object body, final HttpInputMessage inputMessage, final MethodParameter parameter,
      final Type targetType, final Class<? extends HttpMessageConverter<?>> converterType) {
    return body;
  }
}

我进行了调试,发现这个@ControllerAdvice 可以在ControllerAdviceBean.findAnnotatedBeans() 中找到。但是为什么它没有触发我到目前为止还没有找到。

我猜其他人也有类似的问题。 请参阅How to use RequestBodyAdviceSpring RequestBodyAdvice is not picking up by the mock MVC frame work, how ever it is working for ResponseBodyAdvice

【问题讨论】:

  • 嗨。你有没有想过这个?我也有类似的问题。
  • 很遗憾没有。如果你弄明白了,请告诉我。
  • 我实际上是在尝试加载 JsonViewRequestBodyAdvice 实现,如果 Jackson 在类路径上,我发现它会自动添加。但是,在发现这一点时,我确实找到了一种解决方案。见下文。
  • 只有在您发送帖子请求时才会触发此建议

标签: spring-mvc


【解决方案1】:

在你的控制器方法中尝试用@RequestBody注释方法参数。

例如

@RestController
public class MyController{

   @RequestMapping(.......)
   public MyResponse greetings(@RequestBody MyRequest requestObject){
        //implementation
   }

}

RequestResponseBodyMethodProcessor 类(及其基类AbstractMessageConverterMethodArgumentResolver)负责调用RequestBodyAdvice 的各种抽象方法(beforeBodyReadafterBodyRead 等)。 RequestMappingHandlerAdapter 将选择 RequestResponseBodyMethodProcessor 来处理请求,仅当控制器方法的参数用@RequestBody 进行注释。我们可以看到在RequestResponseBodyMethodProcessorsupportsParameter方法中实现了这个逻辑。

我认为另一种方法是通过扩展 RequestResponseBodyMethodProcessor覆盖它的supportsParameter 方法来创建我们的自己的 MethodProcessor把我们自己的逻辑。不过,我还没有测试过。

【讨论】:

  • 没有请求正文的 RequestMapping 怎么样。
【解决方案2】:

您应该能够使用 DelegatingWebMvcConfiguration 来设置 RequestBodyAdvice。

@Configuration
public class WebConfig extends DelegatingWebMvcConfiguration {

    public RequestBodyAdvice myRequestBodyAdvice(){
        return new MyRequestBodyAdvice();
    }


    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = 
super.requestMappingHandlerAdapter();
        adapter.setRequestBodyAdvice(Arrays.asList(myRequestBodyAdvice()));
        return adapter;
    }
}

请参阅Spring Framework Docs 中的第 22.16.13 节

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多