2013-11-21 1 views
2

내가 서버에 요청을 보내저지 : javax.ws.rs.core.Response에의 MediaType를 설정하는 방법, 실제 "실제"응답 헤더는

... 
Response response = builder.method(req.getMethod(), Entity.entity(req, req.getMediaType())); // req.getMediaType() return MediaType.APPLICATION_XML 
if(response.getStatus() != 200) 
    throw new CoreErrorException("core resulted error with status = " + response.getStatus()); 
T resp = response.readEntity(respType); 
... 

저지 던져 예외에 대한 정보가없는 경우 마지막 줄에 :

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/octet-stream 

나는 약간 조사를했다. 우선, 나는 그 반응을 잡는다 :

Content-Length: 93 
Date: Thu, 21 Nov 2013 12:53:46 GMT 
Server: APP 

<root> 
    <returncode>XXX</returncode> 
    <desc>some description</desc> 
</root> 

헤더에는 MediaType에 대한 정보가 없다. 실제로

, 내가 response.getMediaType를 호출하려고(), 그것은 를 반환합니다.

나는 그것이 문제라고 생각한다. Jersey는 응답의 MediaType을 감지하지 못하고 기본적으로 설정합니다 ("application/octet-stream"). 그러나 실제로 내 답변의 본문에는 XML이 있습니다. 저지에 대해 말할 수있는 방법이 있습니까? 서버가 응답 Content-Type 헤더를 추가하지 않습니다 여부를

답변

6

WebTarget#request(MediaType)를 호출하여 요청에

ClientBuilder.newClient() 
    .target("mytarget") 
    .request("application/xml") 
    .method(req.getMethod(), Entity.entity(req, req.getMediaType())); 

Accept 헤더를 추가하려고합니다. 그렇지 않은 경우 WriterInterceptor을 사용하거나 단순히

을 호출하여 헤더를 변경하려고 할 수 있습니다.
Response response = ...; 
response.getStringHeaders().putSingle(HttpHeaders.CONTENT_TYPE, "text/plain"); 
T resp = response.readEntity(respType); 
+0

감사합니다. 두 번째 해결책은 나에게 도움이되었다. – Sergey

관련 문제