带有 MessageBodyWriter 的 JerseyTest 客户端不将 headers 传递给我的 MessageBodyReader server-side
JerseyTest Client with MessageBodyWriter don't pass headers to my MessageBodyReader server-side
我的 headers 在使用 JerseyTest
时没有从客户端正确传递到服务器。
服务器端和客户端都配置了MessageBodyWriter
和MessageBodyReader
,分别配置为JerseyTest::configure
和JerseyTest::configureClient
。
我的 MessageBodyWriter
添加一些自定义 headers 查询 :
@Override
public void writeTo(MyPojo myPojo, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
httpHeaders.put("X-Custom-Header", singletonList(myPojo.header()));
}
但是当MessageBodyReader
被服务器命中时,它只包含标准的headers。
关于我遗漏了什么的任何线索?
好的 InMemoryConnector
似乎有一个错误:
public ClientResponse apply(final ClientRequest clientRequest) {
PropertiesDelegate propertiesDelegate = new MapPropertiesDelegate();
final ContainerRequest containerRequest = new ContainerRequest(baseUri,
clientRequest.getUri(), clientRequest.getMethod(),
null, propertiesDelegate);
containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());
final ByteArrayOutputStream clientOutput = new ByteArrayOutputStream();
if (clientRequest.getEntity() != null) {
clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
@Override
public OutputStream getOutputStream(int contentLength) throws IOException {
final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();
if (contentLength != -1 && !clientHeaders.containsKey(HttpHeaders.CONTENT_LENGTH)) {
containerRequest.getHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
}
return clientOutput;
}
});
所有标准 headers 设置为:
containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());
但是如果您提供自定义 headers,它们会被读取但不会添加到 containerRequest
:
final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();
也https://java.net/jira/browse/JERSEY-2341建议不会很快修复。
我的 headers 在使用 JerseyTest
时没有从客户端正确传递到服务器。
服务器端和客户端都配置了MessageBodyWriter
和MessageBodyReader
,分别配置为JerseyTest::configure
和JerseyTest::configureClient
。
我的 MessageBodyWriter
添加一些自定义 headers 查询 :
@Override
public void writeTo(MyPojo myPojo, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
httpHeaders.put("X-Custom-Header", singletonList(myPojo.header()));
}
但是当MessageBodyReader
被服务器命中时,它只包含标准的headers。
关于我遗漏了什么的任何线索?
好的 InMemoryConnector
似乎有一个错误:
public ClientResponse apply(final ClientRequest clientRequest) {
PropertiesDelegate propertiesDelegate = new MapPropertiesDelegate();
final ContainerRequest containerRequest = new ContainerRequest(baseUri,
clientRequest.getUri(), clientRequest.getMethod(),
null, propertiesDelegate);
containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());
final ByteArrayOutputStream clientOutput = new ByteArrayOutputStream();
if (clientRequest.getEntity() != null) {
clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
@Override
public OutputStream getOutputStream(int contentLength) throws IOException {
final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();
if (contentLength != -1 && !clientHeaders.containsKey(HttpHeaders.CONTENT_LENGTH)) {
containerRequest.getHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
}
return clientOutput;
}
});
所有标准 headers 设置为:
containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());
但是如果您提供自定义 headers,它们会被读取但不会添加到 containerRequest
:
final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();
也https://java.net/jira/browse/JERSEY-2341建议不会很快修复。