将RestTemplate响应错误地处理到对象中

时间:2018-02-28 09:23:12

标签: java spring-boot resttemplate

我在将RestTemplate的响应解析为我定义的类的实例时遇到了一些困难。

当我来处理结果时,它 给我数组中正确数量的项目,但它们都设置为默认值 - 它似乎无法处理每个项目的实际内容进入回复。

这是我的班级:

public class Authority {

    private long localAuthorityId;
    private String localAuthorityIdCode;
    private String name;
    private String friendlyName;
    private String url;
    private String schemeUrl;
    private String email;
    private String regionName;
    private String fileName;
    private String fileNameWelsh;
    private int establishmentCount;
    private String creationDate;
    private String lastPublishedDate;
    private int schemeType;
    private Link[] links;

    public Authority() {}

    public Authority (long localAuthorityId, String localAuthorityIdCode, String name, String friendlyName, String url, String schemeUrl,
            String email, String regionName, String fileName, String fileNameWelsh, int establishmentCount, String creationDate,
            String lastPublishedDate, int schemeType, Link[] links) {
        this.localAuthorityId = localAuthorityId;
        this.localAuthorityIdCode = localAuthorityIdCode;
        this.name = name;
        this.friendlyName = friendlyName;
        this.url = url;
        this.schemeUrl = schemeUrl;
        this.email = email;
        this.regionName = regionName;
        this.fileName = fileName;
        this.fileNameWelsh = fileNameWelsh;
        this.establishmentCount = establishmentCount;
        this.creationDate = creationDate;
        this.lastPublishedDate = lastPublishedDate;
        this.schemeType = schemeType;
        this.links = links;
    }

    public long getLocalAuthorityId() {
        return this.localAuthorityId;
    }

    public String getLocalAuthorityIdCode() {
        return this.localAuthorityIdCode;
    }

    public String getName() {
        return this.name;
    }

    public String getFriendlyName() {
        return this.friendlyName;
    }

    public String getUrl() {
        return this.url;
    }

    public String getSchemeUrl() {
        return this.schemeUrl;
    }

    public String getEmail() {
        return this.email;
    }

    public String getRegionName() {
        return this.regionName;
    }

    public String getFilename() {
        return this.fileName;
    }

    public String getFileNameWelsh() {
        return this.fileNameWelsh;
    }

    public int getEstablishmentCount() {
        return this.establishmentCount;
    }

    public String getCreationDate() {
        return this.creationDate;
    }

    public String getLastPublishedDate() {
        return this.lastPublishedDate;
    }

    public int getSchemeType() {
        return this.schemeType;
    }

    public Link[] getLinks() {
        return this.links;
    }
}

这是我的响应包装器(我已经确认它只调用空构造函数):

public class MyResponseWrapper {
    private Authority[] authorities;

    public MyResponseWrapper() {
        // this is always being called
    }

    public MyResponseWrapper(Authority[] authorities) {
        this.authorities = authorities;
    }

    public Authority[] getAuthorities() {
        return this.authorities;
    }
}

这就是我使用RestTemplate的方式:

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
ResponseEntity<MyResponseWrapper> res = restTemplate.exchange(url, HttpMethod.GET, entity, MyResponseWrapper.class);

这是我试图解析的JSON(我只需要将id和name参数存储在MyClass中):

{  
   "authorities":[  
      {  
         "LocalAuthorityId":197,
         "LocalAuthorityIdCode":"760",
         "Name":"Aberdeen City",
         "FriendlyName":"aberdeen-city",
         "Url":"http://www.aberdeencity.gov.uk",
         "SchemeUrl":"",
         "Email":"commercial@aberdeencity.gov.uk",
         "RegionName":"Scotland",
         "FileName":"http://ratings.food.gov.uk/OpenDataFiles/FHRS760en-GB.xml",
         "FileNameWelsh":null,
         "EstablishmentCount":1847,
         "CreationDate":"2010-08-17T15:30:24.87",
         "LastPublishedDate":"2018-02-08T00:35:50.717",
         "SchemeType":2,
         "links":[  
            {  
               "rel":"self",
               "href":"http://api.ratings.food.gov.uk/authorities/197"
            }
         ]
      },
      {  
         "LocalAuthorityId":198,
         "LocalAuthorityIdCode":"761",
         "Name":"Aberdeenshire",
         "FriendlyName":"aberdeenshire",
         "Url":"http://www.aberdeenshire.gov.uk/",
         "SchemeUrl":"",
         "Email":"environmental@aberdeenshire.gov.uk",
         "RegionName":"Scotland",
         "FileName":"http://ratings.food.gov.uk/OpenDataFiles/FHRS761en-GB.xml",
         "FileNameWelsh":null,
         "EstablishmentCount":2102,
         "CreationDate":"2010-08-17T15:30:24.87",
         "LastPublishedDate":"2018-02-24T00:34:15.547",
         "SchemeType":2,
         "links":[  
            {  
               "rel":"self",
               "href":"http://api.ratings.food.gov.uk/authorities/198"
            }
         ]
      },
      {  
         "LocalAuthorityId":277,
         "LocalAuthorityIdCode":"323",
         "Name":"Adur",
         "FriendlyName":"adur",
         "Url":"http://www.adur-worthing.gov.uk",
         "SchemeUrl":"",
         "Email":"publichealth.regulation@adur-worthing.gov.uk",
         "RegionName":"South East",
         "FileName":"http://ratings.food.gov.uk/OpenDataFiles/FHRS323en-GB.xml",
         "FileNameWelsh":null,
         "EstablishmentCount":417,
         "CreationDate":"2010-08-17T15:30:24.87",
         "LastPublishedDate":"2018-02-28T00:36:33.987",
         "SchemeType":1,
         "links":[  
            {  
               "rel":"self",
               "href":"http://api.ratings.food.gov.uk/authorities/277"
            }
         ]
      },
      {  
         "LocalAuthorityId":158,
         "LocalAuthorityIdCode":"055",
         "Name":"Allerdale",
         "FriendlyName":"allerdale",
         "Url":"http://www.allerdale.gov.uk",
         "SchemeUrl":"",
         "Email":"environmental.health@allerdale.gov.uk",
         "RegionName":"North West",
         "FileName":"http://ratings.food.gov.uk/OpenDataFiles/FHRS055en-GB.xml",
         "FileNameWelsh":null,
         "EstablishmentCount":1092,
         "CreationDate":"2010-08-17T15:30:24.87",
         "LastPublishedDate":"2018-02-28T00:33:58.31",
         "SchemeType":1,
         "links":[  
            {  
               "rel":"self",
               "href":"http://api.ratings.food.gov.uk/authorities/158"
            }
         ]
      },
      {  
         "LocalAuthorityId":48,
         "LocalAuthorityIdCode":"062",
         "Name":"Amber Valley",
         "FriendlyName":"amber-valley",
         "Url":"http://www.ambervalley.gov.uk",
         "SchemeUrl":"",
         "Email":"envhealth@ambervalley.gov.uk",
         "RegionName":"East Midlands",
         "FileName":"http://ratings.food.gov.uk/OpenDataFiles/FHRS062en-GB.xml",
         "FileNameWelsh":null,
         "EstablishmentCount":972,
         "CreationDate":"2010-08-17T15:30:24.87",
         "LastPublishedDate":"2018-02-28T00:34:14.493",
         "SchemeType":1,
         "links":[  
            {  
               "rel":"self",
               "href":"http://api.ratings.food.gov.uk/authorities/48"
            }
         ]
      },
      {  
         "LocalAuthorityId":334,
         "LocalAuthorityIdCode":"551",
         "Name":"Anglesey",
         "FriendlyName":"anglesey",
         "Url":"http://www.ynysmon.gov.uk",
         "SchemeUrl":"",
         "Email":"iechydyramgylchedd@ynysmon.gov.uk",
         "RegionName":"Wales",
         "FileName":"http://ratings.food.gov.uk/OpenDataFiles/FHRS551en-GB.xml",
         "FileNameWelsh":"http://ratings.food.gov.uk/OpenDataFiles/FHRS551cy-GB.xml",
         "EstablishmentCount":703,
         "CreationDate":"2010-08-17T15:30:24.87",
         "LastPublishedDate":"2018-02-28T00:34:28.853",
         "SchemeType":1,
         "links":[  
            {  
               "rel":"self",
               "href":"http://api.ratings.food.gov.uk/authorities/334"
            }
         ]
      }
   ],
   "meta":{  
      "dataSource":"API",
      "extractDate":"2018-02-28T00:58:26.2673908+00:00",
      "itemCount":6,
      "returncode":"OK",
      "totalCount":6,
      "totalPages":1,
      "pageSize":6,
      "pageNumber":1
   },
   "links":[  
      {  
         "rel":"self",
         "href":"http://api.ratings.food.gov.uk/authorities"
      },
      {  
         "rel":"first",
         "href":"http://api.ratings.food.gov.uk/authorities/1/6"
      },
      {  
         "rel":"previous",
         "href":"http://api.ratings.food.gov.uk/authorities/1/6"
      },
      {  
         "rel":"next",
         "href":"http://api.ratings.food.gov.uk/authorities/1/6"
      },
      {  
         "rel":"last",
         "href":"http://api.ratings.food.gov.uk/authorities/1/6"
      }
   ]
}

我知道在尝试解析此响应时我犯了一个非常基本的错误。如果有人能指出它,真的很感激。欢呼声。

0 个答案:

没有答案
相关问题