无法使用基于 Jersey 的客户端进行 REST 调用

Not able to make REST Calls with Jersey based client

我有一个基于 Jersey 的 REST 客户端和一个如下定义的 GET 调用:

代码在 client.get() 调用后没有出来。 client.get() 调用时出现空指针异常,这是调用 REST 客户端的 class 的初始化逻辑:

Student.java :

public class Student {

    private static RestClient client;

    @SuppressWarnings("static-access")
    public Student(Client client) {
        this.client = new RestClient(client, "xyz");
    }

    public static RestClient getClient() {
        return client;
    }

    public static void addStudent() throws Exception {

        try {       
            String addStudentUri = "https://www.zebapi.com/api/v1/market/ticker-new/btc/inr";     
            ClientResponse js_response = getClient().get(URI.create(addStudentUri));
            String s = js_response.getEntity(String.class);
            System.out.println(s);
        } catch (Exception e) {
            System.out.println(e);          
        }
    }

    public static void main(String[] args) {
        try {
            addStudent();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

RestClient.java

public class RestClient {

    private Client client;
    private String randomHeader = "";

    public RestClient(Client jerseyClient) {
        this.client = jerseyClient;
    }

    public RestClient(Client jerseyClient, String random) {
        this.client = jerseyClient;
        this.randomHeader = random;
    }

    public String getRandomHeader() {
        return randomHeader;
    }

    public void setRandomHeader(String randomHeader) {
        this.randomHeader = randomHeader;
    }

    public RestClient() {
    }

    public ClientResponse get(URI uri)
    {
        return client.resource(uri)
                .get(ClientResponse.class);
    }

    public ClientResponse post(URI uri)
    {
        return client.resource(uri)
                .type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class);
    }

}

你能帮忙吗?

您从未初始化 client,这就是您获得 NPE 的原因;您正在尝试使用尚未初始化的对象。

您应该做的是从所有方法和 client 字段中删除所有 static 修饰符;除了 main 方法。然后在 main 方法中,创建一个新的 Student 并传入一个新的 Client。然后使用那个 Student 实例来调用 addStudent() 方法。

public class Student {
    private RestClient client;

    public Student(Client client) {
        this.client = new RestClient(client, "xyz");
    }

    public RestClient getClient() {
        return this.client;
    }

    public void addStudent() {}

    public static void main(String...args) {
        new Student(Client.create()).addStudent();

        // or a more verbose way
        Client jerseyClient = Client.create();
        Student student = new Student(jerseyClient);
        student.addStudent();
    }
}

我将其标记为社区 Wiki,因为它确实应该标记为 What is a NullPointerException, and how do I fix it? 的副本。这已成为 Stack Overflow 中的标准做法,当一个问题是关于一个易于修复的问题时 NullPointerException,我们应该将问题作为该链接问题的副本关闭。

如果

NullPointerExceptions 是由我们开发人员编写的代码引起的,通常很容易修复。能够检测并解决这个问题应该是微不足道的。因此,请查看链接的问题,以便您下次可以自行处理此类问题。