Reputation: 2144
I am having the spring-data-rest application which was exposed with REST APIs. I am using this API to construct web applications. But I am unable to convert this API response into the POJO for ease of use. I am having the response as follows
{
"_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);
this restTemplate throws me following error
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
}
How to get the Person object from response? I don't want to include _embedded field in my Persion class.
Upvotes: 0
Views: 1766
Reputation: 12184
The return type of the rest response is not Person.class
- it is PagedResources<Person>
.
In order to use RestTemplate
with a generic type you could use the following:
PagedResources<Person> = restTemplate.exchange(
uri,
HttpMethod.GET,
null,
new ParametrizedReturnType()).getBody();
private static final class ParametrizedReturnType extends TypeReferences.PagedResourcesType<Person> {}
Upvotes: 2