如何在没有HTTP请求体的情况下使用@ControllerAdvice?

huangapple 未分类评论48阅读模式
英文:

How to use @ControllerAdvice if there is no HTTP body?

问题

@ControllerAdvice
public class CatchAllRequestsAdvice implements RequestBodyAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(CatchAllRequestsAdvice.class);

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

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

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

    @Override
    public Object handleEmptyBody(
        Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType
    ) {
        LOGGER.info("");
        return body;
    }
}
英文:

I'd like to have an @ControllerAdvice that is called for all HTTP requests on all controllers. Unfortunately it only triggers if there is a HTTP body in the request. If not, it's completely ignored. Isn't spring supposed to call handleEmptyBody() in this case?

Edit
All controllers are annotated with @RestController.

Any ideas?

@ControllerAdvice
public class CatchAllRequestsAdvice implements RequestBodyAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(CatchAllRequestsAdvice.class);

    @Override
    public boolean supports(
        MethodParameter methodParameter, Type targetType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; converterType
    ) {
        LOGGER.info(&quot;&quot;);
        return true;
    }

    @Override
    public HttpInputMessage beforeBodyRead(
        HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; converterType
    ) throws IOException {
        LOGGER.info(&quot;&quot;);
        return inputMessage;
    }

    @Override
    public Object afterBodyRead(
        Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; converterType
    ) {
        LOGGER.info(&quot;&quot;);
        return body;
    }

    @Override
    public Object handleEmptyBody(
        Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; converterType
    ) {
        LOGGER.info(&quot;&quot;);
        return body;
    }
}

huangapple
  • 本文由 发表于 2020年6月29日 16:54:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/62634531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定