Spring RestTemplate自定义映射

时间:2014-09-18 20:15:37

标签: java html json spring rest

我是Spring的新手并跟随http://spring.io/guides/gs/consuming-rest的示例。 我注意到他们还没有映射http://graph.facebook.com/pivotalsoftware中的所有JSON元素,所以我想稍微扩展一下这个例子。对于这个例子,我想添加"喜欢"和" were_here_count",在Page.java中是这样的:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Page {

    private String name;
    private String about;
    private String phone;
    private String website;
    private int were_here_count;
    private int likes;

    public String getName() {return name;}
    public String getAbout() {return about;}
    public String getPhone() {return phone;}
    public String getWebsite() {return website;} 
    public int getVisitCount() {return were_here_count;}
    public int getLikes() {return likes;}
}

并在Application.java中进行这些更改:

import org.springframework.web.client.RestTemplate;

public class Application {

    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();                 
        Page page = restTemplate.getForObject("http://graph.facebook.com/pivotalsoftware", Page.class);
        System.out.println("Name:    " + page.getName());
        System.out.println("About:   " + page.getAbout());
        System.out.println("Phone:   " + page.getPhone());
        System.out.println("Website: " + page.getWebsite());
        System.out.println("Visit count: " + page.getVisitCount());
        System.out.println("Likes: " + page.getLikes());
    }
}

我认为映射是通过元素名称完成的,并且适用于"喜欢"但是没有#34; were_here_count"。输出:

Name:    Pivotal
About:   Pivotal is enabling the creation of modern software applications that leverage big & fast data – on a single, cloud independent platform.
Phone:   (650) 286-8012
Website: http://www.pivotal.io
Visit count: 0
Likes: 1175

were_here_count目前为60.我猜测默认转换器并不像变量名中的下划线。所以我使用了重载版本的getForObject,提供了我自己的映射,如下所示:

package hello;

import java.util.HashMap;
import java.util.Map;
import org.springframework.web.client.RestTemplate;

public class Application {

    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> variables = new HashMap<String, String>(3);
        variables.put("name", "name");
        variables.put("about", "about");
        variables.put("phone", "phone");
        variables.put("website", "website");
        variables.put("were_here_count", "were_here_count");
        variables.put("likes", "likes");

        Page page = restTemplate.getForObject("http://graph.facebook.com/pivotalsoftware", Page.class, variables);
        System.out.println("Name:    " + page.getName());
        System.out.println("About:   " + page.getAbout());
        System.out.println("Phone:   " + page.getPhone());
        System.out.println("Website: " + page.getWebsite());
        System.out.println("Visit count: " + page.getVisitCount());
        System.out.println("Likes: " + page.getLikes());
    }
}

但一切都无济于事。我在这里看到了一些关于自定义JSON转换器的例子,但是对它们并不了解 - 而且,这是一个更简单的例子,我是否可以通过简单的变量名字符串字符串映射来完成这项工作?

任何人都知道如何做到这一点,并愿意告诉我如何构建自定义转换器以及必要的步骤是什么?谢谢! :)

2 个答案:

答案 0 :(得分:4)

尝试将一些Jackson's annotations添加到Page类,以帮助解决JSON的反序列化问题。您应该能够告诉Jackson(默认情况下将在Spring中处理JSON的序列化/反序列化),响应JSON映射到POJO属性的属性。:

public class Page {
    ...
    @JsonProperty("were_here_count") 
    private int wereHereCount;
    ...
}

如果您不确定要返回的属性,另一个选项是将JSON映射到Map

Map<String,Object> map = restTemplate.getForObject("http://graph.facebook.com/pivotalsoftware", Map.class);
for (Map.Entry entry: response.entrySet()){
    // do stuff...
}

有时,当响应JSON被复杂化或者不容易反序列化时,这是进行自定义对象映射的更简单方法。

答案 1 :(得分:0)

您的Page setter是什么样的?它适用于我这个二传手:

public void setWere_here_count(int were_here_count) {
    this.were_here_count = were_here_count;
}
相关问题