为什么以下示例会在请求中吞下我的“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));
}
}
泽西试图修复我的内容编码的幕后是否发生了一些神奇的事情?(它不能因为它是一个封闭源编码,事实上我必须通过查询网络中另一台服务器上的另一个应用程序来解决它。)我什至无法在请求转储中发现“Content-Encoding”标头,这就是为什么我怀疑泽西根本不发送标题。
我当然可以使用一些“X-Content-Encoding”标头,如示例中所示。但是这个解决方案很愚蠢。我已经搜索了各种CommonProperties
, ServerProperties
,ClientProperties
常量池,但没有找到配置选项。