RestTemplate返回null

时间:2018-11-21 17:46:55

标签: rest spring-boot spring-restcontroller spring-rest

我正在尝试使用RestTemplete从Web服务获取信息。当我在浏览器上使用url时,它返回:

   {
      "id": 4242,
      "node_id": "dsgs",
      "name": "keyboard",
      "full_name": "gsdfgs",
      "private": false,
      "owner": 
      {
        "login": "cdss",  
        "gravatar_id": "",

      },

      "description": " fdsfa",
      "fork": false
   }

问题是我不想获取所有这些信息,因此我创建了一个只有几个字段的类

@JsonIgnoreProperties(ignoreUnknown = true)
public class ServiceInfo{

    @JsonProperty("node_id")
    private int nodeId;

    @JsonProperty("full_name")
    private String fullName;

    private String description;

    //setter getters
}

我将服务称为

 RestTemplate restTemplate = new RestTemplate();
 ServiceInfo serviceInfo= restTemplate.getForObject(fullServiceUrl, ServiceInfo.class);

此处serviceInfo对象为null,日志中没有异常。这里有什么问题 ?我是否需要在类中包含服务返回的所有字段?

1 个答案:

答案 0 :(得分:0)

发现了问题,我试图通过http而不是https进行呼叫。