问题描述
我有简单的RestController,它返回CollectionModel:
@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);
}
DemoEntity只有两个字段,即id和name。 SecondEntity相同。 我正在尝试使用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());
}
};
}
}
这里的一件事是我想使用自己的类MyHateoasEntityModel和MyHateoasCollectionModel。它们非常简单:
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());
}
}
public class MyHateoasCollectionModel<T> extends CollectionModel<T> {
public MyHateoasCollectionModel(Iterable<T> content,Link... links) {
super(content,Collections.emptyList());
}
}
问题是,当调用控制器时,将依次调用demoEntityProcessor,demoEntitiesProcessor方法。这就是我想要的应用程序。但是随后,以某种方式调用了secondEntitiesProcessor,但不应该这样。
以前,使用的是弹簧靴1.5.17,一切正常。
以下所有代码:https://github.com/KarinaPleskach/Hateoas
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)