如何使用 RestTEmplate 将 spring-data-rest JSON 响应与其对象映射

how to map spring-data-rest JSON response with its object using RestTEmplate

我有 spring-data-rest 应用程序,该应用程序通过 REST APIs 公开。我正在使用这个 API 来构建网络应用程序。但是我无法将此 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 class.

中包含 _embedded 字段

其余响应的 return 类型不是 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> {}