Java 获取错误:从输入流读取实体时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29503204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error: Error reading entity from input stream
提问by caspersgrin
I have a Jersey REST client talking to (or at least attempting to talk) with PagerDuty (a RESTful https service). I have my client configured for SSL at that seems to be doing it's stuff correctly. But when I tried to connect and access a URI, I get the message: Error reading entity from input streamwhich I presume is on my end, not PagerDuty's.
我有一个 Jersey REST 客户端正在与 PagerDuty(一种 RESTful https 服务)交谈(或至少尝试交谈)。我已经为 SSL 配置了我的客户端,这似乎是正确的。但是,当我尝试连接并访问 URI 时,我收到消息:从输入流读取实体时出错,我认为这是在我的一端,而不是 PagerDuty 的。
Here is my code:
这是我的代码:
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.client.ClientResponse;
public class Harvest {
public static void main(String[] args) throws KeyManagementException,
NoSuchAlgorithmException, NullPointerException {
SSLContext sslContext = SSLContext.getInstance("SSL");
HostnameVerifier
hostnameVerifier=HttpsURLConnection.getDefaultHostnameVerifier();
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[20];
random.nextBytes(bytes);
sslContext.init(null, null, random);
Client client = ClientBuilder.newBuilder()
.sslContext(sslContext)
.hostnameVerifier(hostnameVerifier)
.build();
WebTarget target = client.target("https://peakhosting.pagerduty.com/api/v1/schedules");
Invocation.Builder builder;
builder = target.request();
builder.header("Authorization", "Token token=<secret>");
builder.accept(MediaType.APPLICATION_JSON);
--> ClientResponse response = builder.accept("application/json").get(ClientResponse.class);
System.out.println(response.getStatus());
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
}
}
Any idea what's going on. Perhaps the SSL is screwed up and that's the cause of the problem? In any event, I've put an arrow at the line that generated the exception. Any help will be most definitely be very appreciated.
知道发生了什么。也许 SSL 被搞砸了,这就是问题的原因?无论如何,我在产生异常的那一行放了一个箭头。任何帮助都将非常感谢。
Thanks, Rob
谢谢,罗布
采纳答案by Paul Samsotha
Your problem might be that you are trying to get an instance of ClientResponse
, which is incorrect. With Jersey-/JAX-RS- 2.x, you should instead be using Response
at the return type
您的问题可能是您正在尝试获取 的实例ClientResponse
,这是不正确的。使用 Jersey-/JAX-RS- 2.x,您应该改为使用Response
返回类型
Response response = builder.accept("application/json").get();
If you want to read the entity, you would use
如果你想读取实体,你会使用
MyPojo pojo = response.readEntity(MyPojo.class);
If you don't need any information from the Response
, then you could simply do this
如果您不需要来自 的任何信息Response
,那么您可以简单地执行此操作
MyPojo pojo = builder.accept(...).get(MyPojo.class);
Another thing, for JSON to POJO and vice-versa, make sure you have a JSON Provider, such as this one
另一件事,对于 JSON 到 POJO 和反之亦然,确保你有一个JSON Provider,比如这个
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>${jersey2.version}</version>
</dependency>
回答by Fernando Magalhaes
I have the same problem and this dependency solve it. Thanks!
我有同样的问题,这种依赖解决了它。谢谢!
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.28</version>
</dependency>