Jersey客户端无法反序列化为对象

时间:2018-05-28 03:43:16

标签: java json jersey deserialization jersey-client

所以我正在尝试实现一个简单的 Jersey客户端,它可以访问公共API以获取电影时间等。 https://api.eventcinemas.co.nz/Api/Movies/GetMovies

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

的方法
  1. 字符串
  2. 对象(POJO)
  3. 问题是: JSON to String方法正常工作,将String打印到控制台会给我预期的结果。 build.gradle

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

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

    首先从我的pom.xml依赖项开始......

    的pom.xml

    However when trying to deserialize to my Java Objects I am always getting null.

    我的客户端如下:

    MoviesClient:

    <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>
    

    ,最后是main()类:

    申请类:

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

    会输出以下内容:

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

0 个答案:

没有答案