使用JerseyTest
时,我的标头未正确地从客户端传递到服务器。
服务器和客户端分别配置了MessageBodyWriter
和MessageBodyReader
JerseyTest::configure
和JerseyTest::configureClient
。
我的MessageBodyWriter
添加了一些自定义标题进行查询:
@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
时,它只包含标准标题。
我遗失的任何线索?
答案 0 :(得分:1)
好的似乎是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;
}
});
所有标准标题都设置为:
containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());
但是,如果您提供自定义标头,则会将其读取但不会添加到containerRequest
:
final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();
同时https://java.net/jira/browse/JERSEY-2341建议不会很快修复。