Jersey 2.0 Content-Length未设置

Tod*_*odd 14 java jax-rs jersey moxy

我正在尝试发布到需要使用以下代码设置Content-Length标头的Web服务:

// EDIT: added apache connector code
ClientConfig clientConfig = new ClientConfig();
ApacheConnector apache = new ApacheConnector(clientConfig);

// setup client to log requests and responses and their entities
client.register(new LoggingFilter(Logger.getLogger("com.example.app"), true));

Part part = new Part("123");
WebTarget target = client.target("https://api.thing.com/v1.0/thing/{thingId}");
Response jsonResponse = target.resolveTemplate("thingId", "abcdefg")
                .request(MediaType.APPLICATION_JSON)
                .header(HttpHeaders.AUTHORIZATION, "anauthcodehere")
                .post(Entity.json(part));
Run Code Online (Sandbox Code Playgroud)

从发布说明https://java.net/jira/browse/JERSEY-1617和Jersey 2.0文档https://jersey.java.net/documentation/latest/message-body-workers.html,它暗示Content-长度自动设定.但是,我从服务器返回411响应代码,表明请求中不存在Content-Length.

有谁知道获取Content-Length标头集的最佳方法?

我已通过设置记录器验证了请求中未生成Content-Length标头.

谢谢.

Chr*_*ble 6

我使用Jersey Client 2.2和Netcat进行了快速测试,它向我显示Jersey正在发送Content-Length标头,即使LoggingFilter没有报告它.

为了做这个测试,我首先在一个shell中运行netcat.

nc -l 8090
Run Code Online (Sandbox Code Playgroud)

然后我在另一个shell中执行了以下Jersey代码.

Response response = ClientBuilder.newClient()
    .register(new LoggingFilter(Logger.getLogger("com.example.app"), true))
    .target("http://localhost:8090/test")
    .request()
    .post(Entity.json(IOUtils.toInputStream("{key:\"value\"}")));
Run Code Online (Sandbox Code Playgroud)

运行此代码后,将记录以下行.

INFO: 1 * LoggingFilter - Request received on thread main
1 > POST http://localhost:8090/test
1 > Content-Type: application/json
{key:"value"}
Run Code Online (Sandbox Code Playgroud)

但是,netcat会在消息中报告多个标头.

POST /test HTTP/1.1
Content-Type: application/json
User-Agent: Jersey/2.0 (HttpUrlConnection 1.7.0_17)
Host: localhost:8090
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 13

{key:"value"}
Run Code Online (Sandbox Code Playgroud)

我在OSX上用Java6和Java7运行了这个测试,结果相同.我也在Jersey 2.0中进行了测试,结果相似.