gpt4 book ai didi

java - 休息 HATEOAS : How to serialize nested resources (with Spring HATEOAS)?

转载 作者:行者123 更新时间:2023-12-01 11:10:20 28 4
gpt4 key购买 nike

我使用 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 不是更有意义吗?而不是资源?

最佳答案

扩展资源支持:

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"
}
}
}

但是你必须考虑这是否是连接不同资源的正确选择。除非您在 Controller 方法中提供获取这些嵌入式资源,否则您将无法单独访问它们。一种解决方案是使用 HAL。使用 HAL,您可以使用 _links 属性指向资源,或将此资源嵌入到 _embedded 属性中。

关于java - 休息 HATEOAS : How to serialize nested resources (with Spring HATEOAS)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com