【问题标题】:Why does Jersey swallow my "Content-Encoding" header为什么泽西岛吞下了我的“内容编码”标题
【发布时间】:2013-11-05 16:34:32
【问题描述】:

为什么下面的示例会在请求中吞下我的“Content-Encoding”的 HTTP-Header。我正在编写一个需要解码自定义编码格式的应用程序。但是,我永远无法从请求中获取“Content-Encoding”标头。既不在实际资源中,也不在 ReaderInterceptor 中。在响应中,这个编码头没有被吞掉。

在以下(可运行的)示例中可以很容易地观察到这种行为:

public class Demo extends JerseyTest {
  @Override
  protected Application configure() {
    enable(TestProperties.DUMP_ENTITY);
    enable(TestProperties.LOG_TRAFFIC);
    return new ResourceConfig(MyResource.class, MyInterceptor.class);
  }

  public static final String PATH = "path";
  public static final String ENCODING = "my-encoding";
  public static final String CUSTOM_HEADER = "X-Content-Encoding";
  public static final String QUESTION = "question", ANSWER = "answer";

  @Path(PATH)
  public static class MyResource {
    @POST
    public Response handle(String value, @Context HttpHeaders httpHeaders) {
      assertEquals(ENCODING, httpHeaders.getHeaderString(CUSTOM_HEADER));
      // Here, the "Content-Encoding" header mysteriously disappeared.
      assertEquals(ENCODING, httpHeaders.getHeaderString(HttpHeaders.CONTENT_ENCODING));
      return Response
          .ok(ANSWER)
          .header(CUSTOM_HEADER, ENCODING)
          .header(HttpHeaders.CONTENT_ENCODING, ENCODING)
          .build();
    }
  }

  public static class MyInterceptor implements ReaderInterceptor, WriterInterceptor {
    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) 
        throws IOException, WebApplicationException {
      assertEquals(ENCODING, context.getHeaders().getFirst(CUSTOM_HEADER));
      // Here, the "Content-Encoding" header mysteriously disappeared.
      assertEquals(ENCODING, context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
      return context.proceed();
    }

    @Override
    public void aroundWriteTo(WriterInterceptorContext context) 
        throws IOException, WebApplicationException {
      assertEquals(ENCODING, context.getHeaders().getFirst(CUSTOM_HEADER));
      // Here, the "Content-Encoding" header can be found.
      assertEquals(ENCODING, context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
      context.proceed();
    }
  }

  @Test
  public void test() throws Exception {
    Response response = target(PATH)
        .request()
        .header(CUSTOM_HEADER, ENCODING)
        .header(HttpHeaders.CONTENT_ENCODING, ENCODING)
        .post(Entity.text(QUESTION));
    assertEquals(200, response.getStatus());
    assertEquals(ENCODING, response.getHeaders().getFirst(CUSTOM_HEADER));
    // Here, the "Content-Encoding" header can be found.
    assertEquals(ENCODING, response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
  }
}

Jersey 试图修复我的内容编码的幕后是否发生了一些魔法? (它不能因为它是一个封闭源编码,事实上我必须通过查询网络中另一台服务器上的另一个应用程序来解决它。)我什至无法在请求转储中发现“Content-Encoding”标头,这就是为什么我怀疑泽西根本不发送标头。

我当然可以使用一些“X-Content-Encoding”标头,如示例中所示。但是这个解决方案很愚蠢。我已经搜索了各种CommonProperties、ServerProperties、ClientProperties 常量池,但没有找到配置选项。

【问题讨论】:

    标签: jersey jax-rs jersey-client


    【解决方案1】:

    您看到的问题是因为您正在使用

    有效地覆盖 Content-Encoding 标头
    .post(Entity.text(QUESTION));
    

    打电话。 Entity.text(...) 方法生成内容数据 Variant 字段设置为的实体:

    media type = "text/plain";
    content language = null;
    content encoding = null;
    

    这些内容编码和语言的null 值反过来会删除任何先前设置的Content-Encoding 或Content-Language 标头。要解决此问题,您需要将内容编码指定为实体的一部分:

        Response response = target(PATH)
                .request()
                .post(Entity.entity(QUESTION, 
                      new Variant(MediaType.TEXT_PLAIN_TYPE, (String) null, "my-encoding")));
    

    (我同意这种行为有点令人困惑,因为它并不明显。也许我们应该修复 Jersey 以不覆盖带有 null 变量字段值(如果设置)的标题...)

    【讨论】:

    • 不幸的是,在泽西岛改变覆盖行为似乎并不容易。 post 方法的 JAX-RS javadoc 清楚地说明:之前设置的任何与变体相关的 HTTP 标头(即 Content-Type、Content-Language 和 Content-Encoding)都将被实体变体信息覆盖。
    • 我同意你的建议。在我的应用程序中,我使用Entity 来指定自定义MediaType。我一直想知道为什么没有明确的方法来设置内容编码,但我想这就是原因。谢谢!
    • 对于未来的读者:我通过JerseyTest#configureClient(ClientConfig) 在我的单元测试中添加了WriterInterceptor。这样做,我避免了很多样板。由于拦截器在调用RequestBuilder#post(Entitiy) 之后 被调用,因此我可以添加请求标头并在解析Entitiy 时覆盖隐式设置的标头。
    • 但是,在我的单元测试之外使用它不起作用:stackoverflow.com/questions/19812967/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2014-06-10
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多