RestTemplate + Jackson [英] RestTemplate + Jackson

查看:724
本文介绍了RestTemplate + Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Spring的RestTemplate和Jackson来使用WebService。我已经按照几个教程进行了创建DAO。这是我获取所有域对象的方法:

I want to use Spring's RestTemplate plus Jackson to consume a WebService. I have followed several tutorials and have come to the point of creating the DAOs. This is the method where I get all of my domain objects:

// Create a Rest template
RestTemplate restTemplate = new RestTemplate();

// Create a list for the message converters

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();

// Add the Jackson Message converter
messageConverters.add(new MappingJacksonHttpMessageConverter());

// Add the message converters to the restTemplate
restTemplate.setMessageConverters(messageConverters);

List<Station> resultList = Arrays.asList(restTemplate.getForObject(BASE_URL, Station[].class));

return resultList;

但是我的Web服务不会立即返回一个Station对象数组,而是更多的语义表达式以这种方式:

But my Web Service does not return an array of Station objects right away, but rather a more semantic expression in this way:

{"success":true,"message":"Records Retrieved Successfully","data":{"totalCount":"14","stations":[{"id":"1264","station":"Station 1","idJefatura":"1","syncDate":"2013-01-24 13:20:43"}, ...] }}

所以我的问题是,我是不知道如何告诉RestTemplate在站点指示符之后立即解析对象列表,而不创建一个ad hoc对象,这似乎不是正确的解决方案。

So my problem is, I'm not sure how to "tell" RestTemplate to parse the object list right after the "stations" indicator, without creating an ad hoc object, which does not seem like the proper solution.

有没有办法为RestTemplate指定正确的语法?

Is there any way to specify the right syntax for RestTemplate?

编辑:我创建了一个这样的包装器对象:

I created a wrapper object like this:

public class RestResponseObject {

    private boolean success;
    private String message;
    private Data data;

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public class Data {
        private int totalCount;
        private List<Station> stations;

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        public List<Station> getStations() {
            return stations;
        }

        public void setStations(List<Station> estaciones) {
            this.stations= estaciones;
        }
    }
}

但我正在努力如何使这个对象通用,因为JSON响应中我的对象列表的键名依赖于该域对象的类。

But I am struggling as to how to make this object generic, since the key name of my object list in the JSON response is dependant of that domain object's class.

推荐答案

这里有两个解决方案:


  1. 您可以编写自己的反序列化器实现,在这里您解析JSON并仅获取站列表和将其转换为List对象。可以在RestTemplate上设置反序列化器。看看如何为杰克逊写自定义解析器

  2. 另外一件事你可以做的是编写一个映射Rest响应的类。此类应包含List对象作为成员变量。然后Spring默认会转换为新类,你可以从该类中获取。

这是一个例子。

响应类

public class MyResponseClass {
      // other variables
     private List<Station> stations; //it getters and setters
}

在Rest Client中

In the Rest Client

MyResponseClass response = restTemplate.getForObject(BASE_URL, MyResponseClass.class)
List<Station> resultList = response.getStations()

这篇关于RestTemplate + Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆