Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 使用hateoas.server.RepresentationModelProcessor中hateoas.CollectionModel的自定义继承器_Spring Boot_Hateoas - Fatal编程技术网

Spring boot 使用hateoas.server.RepresentationModelProcessor中hateoas.CollectionModel的自定义继承器

Spring boot 使用hateoas.server.RepresentationModelProcessor中hateoas.CollectionModel的自定义继承器,spring-boot,hateoas,Spring Boot,Hateoas,我有一个返回CollectionModel的简单RestController: @RequestMapping("/test") public ResponseEntity<?> index() { List<DemoEntity> all = Arrays.asList(new DemoEntity(1L, "first"),

我有一个返回CollectionModel的简单RestController:

    @RequestMapping("/test")
    public ResponseEntity<?> index() {
        List<DemoEntity> all = Arrays.asList(new DemoEntity(1L, "first"),
                                            new DemoEntity(2L, "second"),
                                            new DemoEntity(3L, "third"));
        List<EntityModel<DemoEntity>> list = new ArrayList<>();
        all.forEach(demoEntity -> list.add(new EntityModel<>(demoEntity)));
        CollectionModel<EntityModel<DemoEntity>> collectionModel = new CollectionModel<>(list);
        return ResponseEntity.ok(collectionModel);
    }
@RequestMapping(“/test”)
公众反应指数(){
List all=Arrays.asList(新的DemoEntity(1L,“第一”),
新DemoEntity(2L,“第二”),
新人口实体(3L,“第三”);
列表=新的ArrayList();
all.forEach(demoEntity->list.add(newentitymodel(demoEntity));
CollectionModel CollectionModel=新的CollectionModel(列表);
返回ResponseEntity.ok(collectionModel);
}
DemoEntity只有两个字段,id和name。第二个实体是相同的。 我正在尝试使用RepresentationModelProcessor:

@Configuration
public class SpringDataRestConfig {

    @Bean
    public RepresentationModelProcessor<EntityModel<DemoEntity>> demoEntityProcessor() {
        return new RepresentationModelProcessor<EntityModel<DemoEntity>>() {
            @Override
            public EntityModel<DemoEntity> process(EntityModel<DemoEntity> entity) {
                return new MyHateoasEntityModel<>(entity.getContent(), entity.getLink("self").orElse(new Link("self")));
            }
        };
    }

    @Bean
    public RepresentationModelProcessor<CollectionModel<EntityModel<DemoEntity>>> demoEntitiesProcessor() {

        return new RepresentationModelProcessor<CollectionModel<EntityModel<DemoEntity>>>() {

            @Override
            public CollectionModel<EntityModel<DemoEntity>> process(CollectionModel<EntityModel<DemoEntity>> collection) {
//                return new CollectionModel<>(collection.getContent());
                return new MyHateoasCollectionModel<>(collection.getContent());
            }
        };
    }

    @Bean
    public RepresentationModelProcessor<EntityModel<SecondEntity>> secondEntityProcessor() {
        return new RepresentationModelProcessor<EntityModel<SecondEntity>>() {
            @Override
            public EntityModel<SecondEntity> process(EntityModel<SecondEntity> entity) {
                return new MyHateoasEntityModel<>(entity.getContent(), entity.getLink("self").orElse(new Link("self")));
            }
        };
    }

    @Bean
    public RepresentationModelProcessor<CollectionModel<EntityModel<SecondEntity>>> secondEntitiesProcessor() {

        return new RepresentationModelProcessor<CollectionModel<EntityModel<SecondEntity>>>() {

            @Override
            public CollectionModel<EntityModel<SecondEntity>> process(CollectionModel<EntityModel<SecondEntity>> collection) {
//                return new CollectionModel<>(collection.getContent());
                return new MyHateoasCollectionModel<>(collection.getContent());
            }
        };
    }

}
@配置
公共类SpringDataRestConfig{
@豆子
public RepresentationModelProcessor DemonityProcessor(){
返回新的RepresentationModelProcessor(){
@凌驾
公共EntityModel流程(EntityModel实体){
返回新的MyHateoasEntityModel(entity.getContent(),entity.getLink(“self”).orElse(newlink(“self”));
}
};
}
@豆子
public RepresentationModelProcessor Demonities Processor(){
返回新的RepresentationModelProcessor(){
@凌驾
公共CollectionModel流程(CollectionModel集合){
//返回新的CollectionModel(collection.getContent());
返回新的MyHateoasCollectionModel(collection.getContent());
}
};
}
@豆子
public RepresentationModelProcessor secondEntityProcessor(){
返回新的RepresentationModelProcessor(){
@凌驾
公共EntityModel流程(EntityModel实体){
返回新的MyHateoasEntityModel(entity.getContent(),entity.getLink(“self”).orElse(newlink(“self”));
}
};
}
@豆子
public RepresentationModelProcessor SecondEntitysProcessor(){
返回新的RepresentationModelProcessor(){
@凌驾
公共CollectionModel流程(CollectionModel集合){
//返回新的CollectionModel(collection.getContent());
返回新的MyHateoasCollectionModel(collection.getContent());
}
};
}
}
这里有一件事是我想使用我自己的类MyHateoasEntityModel和MyHateOASECollectionModel。它们很简单:

public class MyHateoasEntityModel<T> extends EntityModel<T> {
    private T entity;

    public MyHateoasEntityModel(T entity, Iterable<Link> links) {
        super(entity, Collections.emptyList());
        this.entity = entity;
    }
    public MyHateoasEntityModel(T entity, Link... links) {
        this(entity, Collections.emptyList());
    }
}
公共类MyHateoasEntityModel扩展了EntityModel{
私营T实体;
公共MyHateoasEntityModel(T实体,Iterable链接){
super(entity,Collections.emptyList());
this.entity=实体;
}
公共MyHateoasEntityModel(T实体、链接…链接){
这个(实体,Collections.emptyList());
}
}
公共类MyHateoasCollectionModel扩展了CollectionModel{
公共MyHateoasCollectionModel(可编辑内容、链接…链接){
super(content,Collections.emptyList());
}
}
问题是,在调用控制器时,依次调用demoEntityProcessor和demoEntityProcessor方法。这就是我想从应用程序中得到的。但是,不知何故,secondEntitiesProcessor被调用了,但不应该

早些时候,使用了spring boot 1.5.17,一切正常

所有代码位于:

public class MyHateoasCollectionModel<T> extends CollectionModel<T> {
    public MyHateoasCollectionModel(Iterable<T> content, Link... links) {
        super(content, Collections.emptyList());
    }
}