【问题标题】:how to map spring-data-rest JSON response with its object using RestTEmplate如何使用 RestTEmplate 将 spring-data-rest JSON 响应与其对象映射
【发布时间】:2017-02-20 22:48:11
【问题描述】:

我拥有使用 REST API 公开的 spring-data-rest 应用程序。我正在使用这个 API 来构建 Web 应用程序。但是为了便于使用,我无法将此 API 响应转换为 POJO。我的回复如下

    {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons{&sort,page,size}", 
      "templated" : true
    },
    "next" : {
      "href" : "http://localhost:8080/persons?page=1&size=5{&sort}", 
      "templated" : true
    }
  },
  "_embedded" : {
    "person": {
       "id": 1,
       "name": "John"
    }
  },
  "page" : { 
    "size" : 5,
    "totalElements" : 50,
    "totalPages" : 10,
    "number" : 0
  }
}

restTemplate.getForObject(uri, Person.class);

这个 restTemplate 引发了以下错误

    22:50:10.377 [http-bio-8080-exec-28] DEBUG c.o.x.o.accessor.XWorkMethodAccessor - Error calling method through OGNL: object: [com.foo.supply.actions.ViewPersonsAction@9756ac3] method: [viewPersons] args: [[]]
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "_embedded" (Class com.foo.support.model.Person), not marked as ignorable 
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@d4aff35; line: 2, column: 18] (through reference chain: com.foo.support.model.Person["_embedded"]); nested exception is org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "_embedded" (Class com.foo.support.model.Person), not marked as ignorable 
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@d4aff35; line: 2, column: 18] (through reference chain: com.foo.support.model.Person["_embedded"])
        at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:127) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
        at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
        at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:81) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
        at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]

Person.java

public class Person {
    private int id;
    private String name;

    // getters and setters
}

如何从响应中获取 Person 对象?我不想在我的 Persion 类中包含 _embedded 字段。

【问题讨论】:

    标签: spring spring-data spring-data-rest spring-hateoas


    【解决方案1】:

    rest 响应的返回类型不是Person.class - 它是PagedResources<Person>

    要将RestTemplate 与泛型类型一起使用,您可以使用以下内容:

        PagedResources<Person> = restTemplate.exchange(
                        uri,
                        HttpMethod.GET,
                        null,
                        new ParametrizedReturnType()).getBody();
    
    private static final class ParametrizedReturnType extends TypeReferences.PagedResourcesType<Person> {}
    

    【讨论】:

    • 感谢您的回答。但我的 PagedResource 类型是动态的。也就是说,它可能是人、员工或访客。我不想为所有这些声明单独的类。因为响应可能是 PagedResource 或 Resource
    猜你喜欢
    • 2019-07-22
    • 2018-05-16
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2016-11-25
    • 2017-01-19
    相关资源
    最近更新 更多