![](/img/trans.png)
[英]How to consume Spring HATEOAS REST resources containing links to another resources?
[英]REST HATEOAS: How to serialize nested resources (with Spring HATEOAS)?
我使用Spring HATEOAS在我的應用程序中創建REST HATEOAS API。 到目前為止,它運行良好,但是在嵌套資源方面我還是很困惑。 將這樣的類層次結構映射到REST HATEOAS資源的正確方法是:
public class MyEntity {
private int id;
private List<ChildEntity> children;
}
public class ChildEntity {
private int id;
private AnotherEntity entity;
}
public class AnotherEntity {
}
我為所有這些實體創建了Resource類,但是在序列化MyEntity時,所有包含的實體都被序列化為POJO,盡管我也需要將它們序列化為資源(帶有鏈接等)。 有沒有一種方法可以將資源添加到父資源(而不使用Resources類)? 還是我必須向孩子添加@JsonIgnore,然后在ResourceAssembler中手動將孩子添加為資源? 那么使用ResourceSupport而不是Resource更有意義嗎?
擴展ResourceSupport:
public class MyEntityResource extends ResourceSupport {
private int identificator;
private List<ChildEntityResource> children;
public int getIdentificator() {
return identificator;
}
public void setIdentificator(int id) {
this.identificator = identificator;
}
public List<ChildEntityResource> getChildren() {
return children;
}
public void setChildren(List<ChildEntityResource> children) {
this.children = children;
}
}
public class ChildEntityResource extends ResourceSupport {
private int identificator;
private AnotherEntityResource entity;
public int getIdentificator() {
return identificator;
}
public void setIdentificator(int identificator) {
this.identificator = identificator;
}
public AnotherEntityResource getEntity() {
return entity;
}
public void setEntity(AnotherEntityResource entity) {
this.entity = entity;
}
}
public class AnotherEntityResource extends ResourceSupport {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@RestController
public class EntityController {
@RequestMapping(value = "/entity", method = RequestMethod.GET)
public ResponseEntity<MyEntityResource> index() {
AnotherEntityResource anotherEntityResource = new AnotherEntityResource();
anotherEntityResource.add(new Link("link-for-another-entity-resource", "rel-1"));
anotherEntityResource.setValue("value for Another Entity","rel-2");
ChildEntityResource childEntityResource = new ChildEntityResource();
childEntityResource.setIdentificator(20);
childEntityResource.setEntity(anotherEntityResource);
childEntityResource.add(new Link("link-for-child-entity-resource", "rel-3"));
MyEntityResource entityResource = new MyEntityResource();
entityResource.setIdentificator(100);
entityResource.setChildren(Arrays.asList(childEntityResource));
entityResource.add(new Link("link-for-entity-resource"));
return new ResponseEntity<MyEntityResource>(entityResource, HttpStatus.OK);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
結果是:
{
"identificator": 100,
"children": [
{
"identificator": 20,
"entity": {
"value": "value for Another Entity",
"_links": {
"rel-1": {
"href": "link-for-another-entity-resource"
}
}
},
"_links": {
"rel-2": {
"href": "link-for-child-entity-resource"
}
}
}
],
"_links": {
"rel-3": {
"href": "link-for-entity-resource"
}
}
}
但是您必須考慮這是否是連接不同資源的正確選擇。 除非您在控制器中提供方法來獲取此嵌入式資源,否則您將無法單獨訪問它們。 一種解決方案是使用HAL。 使用HAL,您可以使用_links屬性指向資源,也可以將此資源嵌入_embedded屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.