【问题标题】:REST HATEOAS: How to serialize nested resources (with Spring HATEOAS)?REST HATEOAS:如何序列化嵌套资源(使用 Spring HATEOAS)?
【发布时间】:2015-12-03 19:19:07
【问题描述】:

我使用 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会更有意义吗?

【问题讨论】:

    标签: java rest spring-hateoas hateoas


    【解决方案1】:

    扩展资源支持:

    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 属性中。

    【讨论】:

      猜你喜欢
      • 2014-11-09
      • 1970-01-01
      • 2013-07-15
      • 2016-06-13
      • 1970-01-01
      • 2023-03-29
      • 2011-11-02
      • 2014-03-07
      • 2013-10-31
      相关资源
      最近更新 更多