如何在Spring Data REST中公开@EmbeddedId转换器

for*_*rtm 7 java spring spring-data spring-data-rest

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

点击链接也会出现这样的错误 -

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type com.core.connection.domains.UserFriendshipId
Run Code Online (Sandbox Code Playgroud)

我有XML配置的Spring Repository和jpa:启用了存储库,还有从JpaRepository扩展的Respository

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

_links: {
userByFriendshipId: {
href: "http://localhost:8080/api/userFriendships/com.core.connection.domains.UserFriendshipId@5b10/userByFriendId"
}
Run Code Online (Sandbox Code Playgroud)

在xml配置中,我在存储库中启用了jpa:repositories和@RestResource

a b*_*ver 4

首先,您需要获得一个可用的链接。目前您的复合 ID 公开为com.core.connection.domains.UserFriendshipId@5b10. toString重写的 方法应该足以UserFriendshipId生成诸如 之类的有用内容2-3

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

class UserFriendShipIdConverter implements Converter<String, UserFriendshipId> {

  UserFriendShipId convert(String id) {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

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

protected void configureConversionService(ConfigurableConversionService conversionService) {
   conversionService.addConverter(new UserFriendShipIdConverter());
} 
Run Code Online (Sandbox Code Playgroud)

如果您更喜欢 XML 配置,可以按照文档中的说明进行操作。