泽西岛从空响应中获取 Headers
Jersey Get Headers from Empty Response
我正在尝试更新我不完全理解的遗留代码库的一部分以使用更新版本的 REST API 我也无权访问其内部结构。我确实有它的一个 Swagger 实例,可以通过 curl
成功调用它,但是要么 Jersey 行为不端,要么我误解了如何阅读某些东西。
如果我执行以下 curl
命令:
curl -v -k -X POST "[api endpoint]" -H "accept: application/json" -H "Authorization: Bearer [jwt token]" -H "Content-Type: multipart/form-data" -F "sender=[email address]" -F "recipient=[email address]" -F "fileType=file" -F "data=@[file]" -F "metaData=[other file]"
我收到以下回复:
> Content-Length: [#]
> Content-Type: multipart/form-data; boundary=------------------------9b1405ed70c2fd40
>
} [5 bytes data]
* We are completely uploaded and fine
{ [5 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 201 Created
< Date: Wed, 26 Aug 2020 00:29:56 GMT
< Cache-Control: no-cache, no-store, must-revalidate
< Pragma: no-cache
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Set-Cookie: JSESSIONID=[ID];Path=/;Secure
< Keep-Alive: timeout=600
< X-Frame-Options: SAMEORIGIN
< Server: WildFly
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Connection: Keep-Alive
< Access-Control-Allow-Headers: Content-Type
< Content-Type: application/json
< Location: [value I care about]
< Content-Length: 0
<
100 1667 0 0 100 1667 0 1825 --:--:-- --:--:-- --:--:-- 1825
* Connection #0 to host [proxy] left intact
这对我来说意味着 POST 具有空主体但非空 headers 的响应是有效的。但是,当我尝试通过 Jersey 实现同样的效果时:
ClientConfig config = new ClientConfig();
config.register(MultiPartFeature.class);
Client client = ClientBuilder.newClient(config);
WebTarget endpoint = client.target([uri]);
FormDataMultiPart post = new FormDataMultiPart()
.field("sender", [email], MediaType.TEXT_PLAIN_TYPE)
.field("recipient", [email], MediaType.TEXT_PLAIN_TYPE)
.field("fileType", "file", MediaType.TEXT_PLAIN_TYPE)
.field("data", [InputStream], MediaType.APPLICATION_OCTET_STREAM_TYPE)
.field("metaData", [other InputStream], MediaType.APPLICATION_OCTET_STREAM_TYPE);
JsonNode response = endpoint.path([api path])
.request(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + [jwtTokenString])
.post(Entity.entity(post, MediaType.MULTIPART_FORM_DATA_TYPE), JsonNode.class);
我收到 null
响应,尽管我通过检查服务器日志得知 API 命令已成功接收和处理。
使用调试器很长时间后,我确定这是因为 Jersey 要么 return 为空 object 要么在数据为空时抛出异常。 (有关更多上下文,请参阅 。奇怪的是,我无法在我可以通过 Google 找到的任何规范文档中找到他们引用的部分。)
这可能没问题,因为我对响应的空 body 并不感兴趣,但我不知道如何获取 HTTP headers return由于我的 POST 在 Java.
中编辑
您无法获得 headers,因为您使用的方法仅要求 body 作为响应。你要的是整个Response
object。为此,请省略 .post()
方法的最后一个参数 (JsonNode.class)。您正在使用的重载方法表示您只关心响应的 body 之外的任何内容。这就是你会得到的。
当您使用没有最后一个 body 类型参数的重载 post()
方法时,return 类型将为 Response
。您可以从中得到 headers 和 body.
Response response = endpoint.path([api path])
.request(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + [jwtTokenString])
.post(Entity.entity(post, MediaType.MULTIPART_FORM_DATA_TYPE));
URI locationUri = response.getLocation();
String locationHeader = response.getHeaderString("Location");
String body = response.readEntity(String.class);
我正在尝试更新我不完全理解的遗留代码库的一部分以使用更新版本的 REST API 我也无权访问其内部结构。我确实有它的一个 Swagger 实例,可以通过 curl
成功调用它,但是要么 Jersey 行为不端,要么我误解了如何阅读某些东西。
如果我执行以下 curl
命令:
curl -v -k -X POST "[api endpoint]" -H "accept: application/json" -H "Authorization: Bearer [jwt token]" -H "Content-Type: multipart/form-data" -F "sender=[email address]" -F "recipient=[email address]" -F "fileType=file" -F "data=@[file]" -F "metaData=[other file]"
我收到以下回复:
> Content-Length: [#]
> Content-Type: multipart/form-data; boundary=------------------------9b1405ed70c2fd40
>
} [5 bytes data]
* We are completely uploaded and fine
{ [5 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 201 Created
< Date: Wed, 26 Aug 2020 00:29:56 GMT
< Cache-Control: no-cache, no-store, must-revalidate
< Pragma: no-cache
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Set-Cookie: JSESSIONID=[ID];Path=/;Secure
< Keep-Alive: timeout=600
< X-Frame-Options: SAMEORIGIN
< Server: WildFly
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Connection: Keep-Alive
< Access-Control-Allow-Headers: Content-Type
< Content-Type: application/json
< Location: [value I care about]
< Content-Length: 0
<
100 1667 0 0 100 1667 0 1825 --:--:-- --:--:-- --:--:-- 1825
* Connection #0 to host [proxy] left intact
这对我来说意味着 POST 具有空主体但非空 headers 的响应是有效的。但是,当我尝试通过 Jersey 实现同样的效果时:
ClientConfig config = new ClientConfig();
config.register(MultiPartFeature.class);
Client client = ClientBuilder.newClient(config);
WebTarget endpoint = client.target([uri]);
FormDataMultiPart post = new FormDataMultiPart()
.field("sender", [email], MediaType.TEXT_PLAIN_TYPE)
.field("recipient", [email], MediaType.TEXT_PLAIN_TYPE)
.field("fileType", "file", MediaType.TEXT_PLAIN_TYPE)
.field("data", [InputStream], MediaType.APPLICATION_OCTET_STREAM_TYPE)
.field("metaData", [other InputStream], MediaType.APPLICATION_OCTET_STREAM_TYPE);
JsonNode response = endpoint.path([api path])
.request(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + [jwtTokenString])
.post(Entity.entity(post, MediaType.MULTIPART_FORM_DATA_TYPE), JsonNode.class);
我收到 null
响应,尽管我通过检查服务器日志得知 API 命令已成功接收和处理。
使用调试器很长时间后,我确定这是因为 Jersey 要么 return 为空 object 要么在数据为空时抛出异常。 (有关更多上下文,请参阅
这可能没问题,因为我对响应的空 body 并不感兴趣,但我不知道如何获取 HTTP headers return由于我的 POST 在 Java.
中编辑您无法获得 headers,因为您使用的方法仅要求 body 作为响应。你要的是整个Response
object。为此,请省略 .post()
方法的最后一个参数 (JsonNode.class)。您正在使用的重载方法表示您只关心响应的 body 之外的任何内容。这就是你会得到的。
当您使用没有最后一个 body 类型参数的重载 post()
方法时,return 类型将为 Response
。您可以从中得到 headers 和 body.
Response response = endpoint.path([api path])
.request(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + [jwtTokenString])
.post(Entity.entity(post, MediaType.MULTIPART_FORM_DATA_TYPE));
URI locationUri = response.getLocation();
String locationHeader = response.getHeaderString("Location");
String body = response.readEntity(String.class);