spring 休息模板 + 杰克逊

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18328492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:09:45  来源:igfitidea点击:

RestTemplate + Hymanson

springHymansonresttemplate

提问by MichelReap

I want to use Spring's RestTemplate plus Hymanson 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:

我想使用 Spring 的 RestTemplate 加上 Hymanson 来使用 WebService。我已经学习了几个教程,并且已经到了创建 DAO 的地步。这是我获取所有域对象的方法:

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

// Create a list for the message converters

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

// Add the Hymanson Message converter
messageConverters.add(new MappingHymansonHttpMessageConverter());

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

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

return resultList;

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

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

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

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?

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

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

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.

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

回答by Dhanush Gopinath

There are two solutions here:

这里有两种解决方案:

  1. You can write your own Deserializer implementation, where you parse the JSON and take only the station list and convert it to the List object. The Deserializer can be set on the RestTemplate. Have a look at how to write custom desrializer for Hymanson
  2. The other thing what you can do is to write a class which maps the Rest response. This class should contain the List object as a member variable. Then Spring by default will convert to the new class and you can get the stations from that class.
  1. 您可以编写自己的 Deserializer 实现,在其中解析 JSON 并仅获取电台列表并将其转换为 List 对象。可以在 RestTemplate 上设置 Deserializer。看看如何为 Hymanson编写自定义反序列化器
  2. 您可以做的另一件事是编写一个映射 Rest 响应的类。此类应包含 List 对象作为成员变量。然后默认情况下 Spring 将转换为新类,您可以从该类中获取站。

Here is an example.

这是一个例子。

The response class

响应类

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

In the Rest Client

在休息客户端

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