【问题标题】:Spring RestTemplate custom mappingSpring RestTemplate 自定义映射
【发布时间】:2014-11-13 07:40:30
【问题描述】:

我是 Spring 新手,并按照 http://spring.io/guides/gs/consuming-rest 的示例进行操作。 我注意到他们没有映射来自http://graph.facebook.com/pivotalsoftware 的所有 JSON 元素,所以我想稍微扩展一下这个示例。对于这个例子,我想添加“likes”和“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

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 转换器的示例,但并没有很好地理解它们 - 另外,这是一个更简单的示例,我不能通过变量名的简单字符串-字符串映射来完成吗?

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

【问题讨论】:

    标签: java html json spring rest


    【解决方案1】:

    您的 Page 设置器是什么样的?这个设置器对我有用:

    public void setWere_here_count(int were_here_count) {
        this.were_here_count = were_here_count;
    }
    

    【讨论】:

      【解决方案2】:

      尝试将一些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 复杂或不容易反序列化时,有时这是进行自定义对象映射的更简单方法。

      【讨论】:

      • 真的很感谢这个解决方案更简单方便。
      猜你喜欢
      • 2020-02-07
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 2020-07-23
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      相关资源
      最近更新 更多