所以我试图实现一个简单的 Jersey Client ,它可以通过公共API获取电影时间等.https://api.eventcinemas.co.nz/Api/Movies/GetMovies

我已经完成了有关如何执行此操作的教程,并实现了两种将JSON响应反序列化为:

  • 一个字符串

  • 一个对象(POJOs)

The issue is this: JSON to String方法工作正常,将String打印到控制台给我预期的结果 . However when trying to deserialize to my Java Objects I am always getting null.

我尝试了一些简单的事情,如不同的依赖版本,不同的API调用等,但没有运气 . 为了节省时间,我使用在线转换器来获取JSON响应并填充必要的POJO以进行反序列化,我认为这是正确的 .

有人会善意地指出我为什么总是变得无效的正确方向,我觉得我错过了一些小事或傻事 . 提前致谢!

所以从我的pom.xml依赖开始......

pom.xml

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.26</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.26</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

我的客户如下:

MoviesClient:

package nz.co.brownbridge.application;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

public class MoviesClient {

    protected MoviesResponse getMovieDetails() {
        /*JSON to POJO*/
        Client client = ClientBuilder.newClient();
        WebTarget webTarget = client.target("https://api.eventcinemas.co.nz/Api/Movies/GetMovies");
        MoviesResponse response = webTarget.request().accept(MediaType.APPLICATION_JSON_TYPE).get(MoviesResponse.class);
        return response;
    }

    protected String getMovieDetailsString() {
        /*JSON to String*/
        Client client  = ClientBuilder.newClient();
        WebTarget webTarget = client.target("https://api.eventcinemas.co.nz/Api/Movies/GetMovies");
        String response = webTarget.request().accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
        return response;
    }

}

最后是main()类:

Application Class:

package nz.co.brownbridge.application;

public class Application {

    public static void main(String[] args) throws InterruptedException {
        MoviesClient moviesClient = new MoviesClient();

        String stringResponse = moviesClient.getMovieDetailsString();
        MoviesResponse pojoResponse = moviesClient.getMovieDetails();

        System.out.println("Printing String Response...");
        System.out.println();
        System.out.println(stringResponse);
        System.out.println();
        System.out.println();
        System.out.println("Printing POJO Response...");
        System.out.println();
        System.out.println(pojoResponse);


    }
}

Would output the following:

Printing String Response...
//super long but correct string response goes here

Printing POJO Response...

ClassPojo [Data = null, Success = null]