2016-10-12 16 views
0

私はREST APIで公開されていたspring-data-restアプリケーションを使用しています。私はこのAPIを使ってWebアプリケーションを構築しています。しかし、私はこのAPIレスポンスを使いやすさのためにPOJOに変換することができません。私はエラーspring-data-rest JSONレスポンスをRestTEmplateを使用したそのオブジェクトでマップする方法

22:50:10.377 [http-bio-8080-exec-28] DEBUG c.o.x.o.accessor.XWorkMethodAccessor - Error calling method through OGNL: object: [[email protected]] 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: [email protected]f35; 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: [email protected]f35; 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次スロー

{ 
    "_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を次のように私は、応答が生じています

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

    // getters and setters 
} 

どのように応答からPersonオブジェクトを取得するには?私は、私のPersionクラスに_embeddedフィールドを含めたくありません。

答えて

2

残りレスポンスの戻りタイプは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> {} 
+0

は、あなたの答えをいただき、ありがとうございます。しかし、私のPagedResourceタイプは動的です。それは、人または従業員または訪問者である可能性があります。私はこれらのすべてのために別のクラスを宣言したくありません。応答はPagedResource またはリソース SST

関連する問題