我是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());
    }
}


我以为映射是通过元素名称完成的,并且对于“喜欢”有效,但对于“ 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


are_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转换器的示例,但并不太了解它们-另外,这是一个简单得多的示例,我不能通过简单的变量名字符串字符串映射来完成此操作吗?

任何人都知道如何执行此操作,并愿意向我展示如何构建自定义转换器以及必要的步骤是什么?谢谢! :)

最佳答案

尝试在Page类中添加一些Jackson's annotations来帮助反序列化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令人费解或不容易反序列化时进行自定义对象映射的更简单方法。

关于java - Spring RestTemplate自定义映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25921573/

10-16 14:17