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

标签 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

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

我可以让 Repository 实现 org.springframework.core.convert.converter.Converter 来处理这个问题吗?目前正在获取如下链接 -

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

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

最佳答案

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

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

class UserFriendShipIdConverter implements Converter<String, UserFriendshipId> {

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

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

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

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

关于java - 如何在 Spring Data REST 中公开 @EmbeddedId 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26249506/

相关文章:

java - 如何在 spring-data 2.0.x 中创建 RedisCacheManager

java - 获取 Antlr 规则的原始文本

java - 可以用 Go 编写 Intellij 插件吗?

Java - 错误 : A JNI error has occurred, ... 在线程 "main"java.lang.UnsupportedClassVersionError 中:

java - 如何更改Spring Security中SAML请求的签名算法

java - JPA:在空的多边上将获取结果加入到 NULL

java - 从 JPanel 实例化 JDialog

java - 迁移到 spring 框架有多明智?

java - 创建 bean 时出错, Autowiring 失败

java - Spring Mongodb查询DbRef(一对多关系)