【问题标题】:Spring auto-add 'X-Total-Count' header for selected endpointsSpring为选定的端点自动添加“X-Total-Count”标头
【发布时间】:2020-07-02 23:10:13
【问题描述】:

我知道我可以使用 ResponseBodyAdvice 并且它使我能够拦截调用(就在响应被写入并提供对原始 http 响应的访问之前)。喜欢here。 但是我如何配置不应该对所有端点都这样做?

【问题讨论】:

    标签: java spring rest http


    【解决方案1】:

    如果您不喜欢通用方法,那么为什么不直接在您的特定控制器方法中添加自定义响应标头,例如喜欢这里https://www.techiedelight.com/add-header-to-response-spring-boot/。除此之外,您仍然可以使用ResponseBodyAdvice 方法,但必须以编程方式进行过滤,例如通过比较您的 ResponseBodyAdvice 实现中的请求路径 (URL)(在方法 beforeBodyWrite 中 - 您可以访问请求以确定路径):

    T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response)
    

    另请参阅此处的示例:https://mtyurt.net/post/spring-modify-response-headers-after-processing.html

    【讨论】:

      猜你喜欢
      • 2019-12-14
      • 2020-12-14
      • 2021-09-24
      • 2021-04-14
      • 2021-08-20
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      相关资源
      最近更新 更多