HATEOAS链接如果使用JsonSubTypes,则错误

问题描述 投票:0回答:1

我在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数据休息一起使用。

我在Mongo上使用spring数据表来公开具有多个子类型的类。当我这样做时,HATEOAS会根据实际的实例化类型而不是...

spring-data-rest spring-hateoas
1个回答
0
投票

好一整天,@Relation标记在这种情况下似乎根本不起作用。因此,我决定寻找解决方法。我所做的是实现一个RelProvider来正确检查继承,并返回正确的集合名称:

© www.soinside.com 2019 - 2024. All rights reserved.