本文介绍了如何在Spring Data REST中公开@EmbeddedId转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些实体具有复合主键,并且这些实体在公开时具有错误的链接,其中包含URL中的类的完全限定名称_links

There are some Entities with composite Primary Keys and these entities when exposed are having incorrect Links having full qualified name of classes in URL inside _links

同时单击链接这样的错误 -

Also clicking on links gives such errors -

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type com.core.connection.domains.UserFriendshipId

我使用jpa配置了XML配置的Spring存储库:存储库从JpaRepository扩展的enabled和Respository

I have XML configured Spring Repository with jpa:repositories enabled and Respository extending from JpaRepository

我可以使Repository实现 org.springframework.core.convert.converter.Converter 来处理这个问题。目前获得如下链接 -

Can I make Repository implement org.springframework.core.convert.converter.Converter to handle this. Currently getting links as below -

_links: {
userByFriendshipId: {
href: "http://localhost:8080/api/userFriendships/com.core.connection.domains.UserFriendshipId@5b10/userByFriendId"
}

在xml配置中,我有jpa:启用了存储库并且在存储库中启用了@RestResource

in xml config , I have jpa:repositories enabled and @RestResource enabled inside Repositories

推荐答案

你需要获得一个可用的链接。目前,您的复合ID公开为 com.core.connection.domains.UserFriendshipId@5b10 。它应该足以覆盖 UserFriendshipId toString 方法,以生成像 2-这样的有用的东西3

At first you need to get a usable link. Currently your composite id is exposed as com.core.connection.domains.UserFriendshipId@5b10. It should be enough to override the toString method of UserFriendshipIdto produce something useful like 2-3.

接下来你需要实现一个,以便 2-3 可以转换回 UserFriendshipId

Next you need to implement a converter so that 2-3 can be converted back to a UserFriendshipId:

class UserFriendShipIdConverter implements Converter<String, UserFriendshipId> {

  UserFriendShipId convert(String id) {
    ...
  }
}

最后,您需要注册转换器。您已建议覆盖 configureConversionService

Finally you need to register the converter. You already suggested to override configureConversionService:

protected void configureConversionService(ConfigurableConversionService conversionService) {
   conversionService.addConverter(new UserFriendShipIdConverter());
}

如果您更喜欢XML配置,可以按照。

If you prefer a XML configuration you can follow the instructions in the documentation.

这篇关于如何在Spring Data REST中公开@EmbeddedId转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 10:07