我正在Mongo上使用spring数据休息来公开具有多个子类型的类。当我这样做时,HATEOAS将根据实际的实例化类型而不是通用基本类型对结果进行划分。这会导致链接不正确,并且由于分页是混合类型列表,因此使分页无效。
我尝试将@Relation标记显式添加到所有涉及的类中,这似乎完全没有影响。无论有没有,我都能得到相同的结果。
我正在使用带有Spring-boot-starter-data-rest和spring-cloud-dependencies Greenwich.SR1的spring boot依赖2.1.8.RELEASE
基类:
@Relation(collectionRelation = "notifications", value="notifications")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "notificationType")
@JsonSubTypes({
@JsonSubTypes.Type(value = ModelNotification.class, name = Notification.MODEL_NOTIFICATION),
@JsonSubTypes.Type(value = BasicNotification.class, name = Notification.BASIC_NOTIFICATION)
})
public class Notification extends UUIDEntity implements Serializable {
private static final long serialVersionUID = 8199210081144334378L;
public static final String MODEL_NOTIFICATION = "MODEL_NOTIFICATION";
public static final String BASIC_NOTIFICATION = "BASIC_NOTIFICATION";
public enum Severity {
TRACE,
DEBUG,
INFO,
WARN,
ERROR,
FATAL
}
public Notification() {
this.notificationType = BASIC_NOTIFICATION;
}
@JsonProperty("notificationType")
private String notificationType;
@JsonProperty("createdDate")
@CreatedDate
private Instant createdDate;
@JsonProperty("lastModifiedDate")
@LastModifiedDate
private Instant lastModifiedDate;
@JsonProperty("createdBy")
@CreatedBy
private String createdBy;
@JsonProperty("lastModifiedBy")
@LastModifiedBy
private String lastModifiedBy;
@JsonProperty("severity")
private Severity severity;
@JsonProperty("message")
private String message;
没有其他成员版本:
@Relation(collectionRelation = "notifications", value="notifications")
public class BasicNotification extends Notification implements Serializable {
private static final long serialVersionUID = 8063077545983014320L;
}
扩展名:
@Relation(collectionRelation = "notifications", value="notifications")
public class ModelNotification extends Notification implements Serializable {
private static final long serialVersionUID = 3700576594274374440L;
@JsonProperty("storedModel")
private StoredModel storedModel;
public ModelNotification() {
super();
this.setNotificationType(Notification.MODEL_NOTIFICATION);
}
我希望给定添加@Relation标记后,所有结果都将显示在通知下,这是spring数据剩余端点的正确URL。请注意,所有端点均正常工作,但是仅HATEOAS内容不正确,并且捆绑会产生问题。在以下位置访问:/ api / notifications
我回来了:
{
"_embedded" : {
"modelNotifications" : [ {
"notificationType" : "MODEL_NOTIFICATION",
"createdDate" : "2019-10-02T15:53:42.127Z",
"lastModifiedDate" : "2019-10-02T15:53:42.127Z",
...
[SNIP FOR BREVITY]
...
} ]
},
"_links" : {
"self" : {
"href" : "http://fastscore:8088/api/modelNotification/ef81c342-29d3-48fb-bab3-d416e80bc5f6"
},
"modelNotification" : {
"href" : "http://fastscore:8088/api/modelNotification/ef81c342-29d3-48fb-bab3-d416e80bc5f6"
}
}
} ],
"notifications" : [ {
"notificationType" : "BASIC_NOTIFICATION",
"createdDate" : "2019-10-02T15:52:10.261Z",
"lastModifiedDate" : "2019-10-02T15:52:10.261Z",
"createdBy" : "anonymousUser",
"lastModifiedBy" : "anonymousUser",
"severity" : "INFO",
"message" : "Interval Process Completed Successfully",
"_links" : {
"self" : {
"href" : "http://fastscore:8088/api/notifications/93fa5d6b-1457-4fa6-976c-cfdddc422976"
},
"notification" : {
"href" : "http://fastscore:8088/api/notifications/93fa5d6b-1457-4fa6-976c-cfdddc422976"
}
}
...
[SNIP]
...
},
},
"_links" : {
"self" : {
"href" : "http://fastscore:8088/api/notifications{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://fastscore:8088/api/profile/notifications"
},
"search" : {
"href" : "http://fastscore:8088/api/notifications/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 4,
"totalPages" : 1,
"number" : 0
}
}
这显然是不正确的,因为modelNotifications不是spring数据剩余端点。另外,这将使分页变得无用,好像我有大量的通知,分页仅适用于那些通知,即使我只有一个,我也每次都会收到modelNotifications。因此,在第二页上,我将收到第二页的通知,但是即使我只有一个条目,第二页上仍然有modelNotifications。
这种类型的渲染使HATEOAS支持无法与spring数据剩余一起使用。
答案 0 :(得分:0)
整天玩完之后,@ Relation标记似乎在这种情况下根本不起作用。因此,我决定寻找解决方法。我所做的是实现一个RelProvider来正确检查继承,并返回正确的集合名称:
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class NotificationRelProvider implements RelProvider {
@Override
public String getItemResourceRelFor(Class<?> aClass) {
return "notification";
}
@Override
public String getCollectionResourceRelFor(Class<?> aClass) {
return "notifications";
}
@Override
public boolean supports(Class<?> aClass) {
return Notification.class.isAssignableFrom(aClass);
}
}
然后我按照文档中的说明标记了spring数据剩余存储库:
@ExposesResourceFor(Notification.class)
@RepositoryRestResource()
public interface NotificationRepository extends MongoRepository<Notification, UUID> {
List<Notification> findAllBySeverityOrderByCreatedDateDesc(@Param("severity") Notification.Severity severity);
}
完成此操作后,我现在得到以下正确和预期的行为:
"_embedded" : {
"notifications" : [ {
"notificationType" : "BASIC_NOTIFICATION",
"createdDate" : "2019-10-02T23:06:16.802Z",
"lastModifiedDate" : "2019-10-02T23:06:16.802Z",
"createdBy" : "anonymousUser",
"lastModifiedBy" : "anonymousUser",
"severity" : "INFO",
"message" : "Interval Process Completed Successfully",
"_links" : {
"self" : {
"href" : "http://fastscore:8088/api/notifications/eb214276-2880-43e0-8c7f-1519b1e7e343"
},
"notification" : {
"href" : "http://fastscore:8088/api/notifications/eb214276-2880-43e0-8c7f-1519b1e7e343"
}
}
}, {
"notificationType" : "MODEL_NOTIFICATION",
"createdDate" : "2019-10-02T23:07:32.649Z",
"lastModifiedDate" : "2019-10-02T23:07:32.649Z",
"createdBy" : "anonymousUser",
"lastModifiedBy" : "anonymousUser",
....
因此,这可以解决原始错误,但这是一种手动过程,而不仅仅是使用文档中所述的注释。不幸的是,这意味着我必须为存储库中的每个基类创建这些RelProvider类之一,但是至少它可以工作并且可用于分页。