以gs-accessing-data-rest-complete为基础,以http://localhost:8080/people
的格式生成以下格式的人员列表:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/people{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
},
"_embedded" : {
"people" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}, {
"firstName" : "Bilbo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
}
}
}, {
"firstName" : "Bilbo Jr.",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/3"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 3,
"totalPages" : 1,
"number" : 0
}
}
如何在客户端上使用此REST数据?
答案 0 :(得分:0)
经过一番挖掘并尝试Why does RestTemplate not bind response representation to PagedResources?提供的解决方案后,我找到了Jackson2HalIntegrationTest.java中最有效的方法。这是我的实施:
package hello;
import java.io.IOException;
import java.net.URL;
import org.junit.Test;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.hal.Jackson2HalModule;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PersonTest {
private static final String URI_TEMPLATE = "http://localhost:8080/people";
@Test
public void p1() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
mapper.registerModule(new Jackson2HalModule());
URL url = new URL(URI_TEMPLATE);
Resources<Resource<PersonResource>> result = mapper.readValue(url, mapper.getTypeFactory().constructParametricType(Resources.class,
mapper.getTypeFactory().constructParametricType(Resource.class, PersonResource.class)));
for (Resource<PersonResource> resource : result) {
System.out.println(resource.getContent().getFirstName());
System.out.println(resource.getContent().getLastName());
System.out.println(resource.getLink("self").getHref());
System.out.println("=========================================");
}
}
}
和PersonResource.java
package hello;
import org.springframework.hateoas.core.Relation;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
@Relation(value = "person", collectionRelation = "people")
public class PersonResource {
private String firstName;
private String lastName;
public PersonResource() {
}
public PersonResource(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PersonResource other = (PersonResource) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
}