java - 获取错误 : Error reading entity from input stream

标签 java rest ssl jersey jax-rs

我有一个 Jersey REST 客户端与 PagerDuty(一个 RESTful https 服务)交谈(或至少试图交谈)。我已将我的客户端配置为使用 SSL,这似乎是正确的。但是,当我尝试连接和访问 URI 时,我收到消息:从输入流读取实体时出错,我认为这是我的结果,而不是 PagerDuty 的。

这是我的代码:

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());
        }


    }
}

知道发生了什么。也许 SSL 搞砸了,这就是问题的原因?无论如何,我在生成异常的那一行放了一个箭头。非常感谢任何帮助。

谢谢, 罗布

最佳答案

您的问题可能是您正在尝试获取 ClientResponse 的实例,这是不正确的。使用 Jersey-/JAX-RS- 2.x,您应该改为在返回类型处使用 Response

Response response = builder.accept("application/json").get();

如果你想读取实体,你会使用

MyPojo pojo = response.readEntity(MyPojo.class);

如果您不需要Response 中的任何信息,那么您可以简单地这样做

MyPojo pojo = builder.accept(...).get(MyPojo.class);

另一件事,对于 JSON 到 POJO,反之亦然,确保你有一个 JSON Provider ,例如 this one

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

关于java - 获取错误 : Error reading entity from input stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503204/

相关文章:

ssl - 使用 cfssl 和 kubernetes 生成 CA 证书和私钥时出错

google-maps - 使用 SSL 托管 KML 时无法在 Google map 中显示 KML

java - 如何更改标签的值,并在程序关闭后仍保留新值

python - fastapi 设置身份验证 token 基本

angularjs - 使用Nginx作为代理以避免CORS

java - 如何保护移动应用程序的 API REST? (如果嗅探请求为您提供 "key")

java - 在左子右兄弟树中查找第一个元素

java - 以编程方式关闭 BlueCove 堆栈

java - 在 Java 中反复打开和关闭 PrintWriter 对象是不好的做法吗?

ssl - 使用 cert-manager istio ingress 和 LetsEncrypt 在 kubernetes 中配置 SSL 证书