gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:27:35 24 4
gpt4 key购买 nike

有些实体具有复合主键,这些实体在暴露时具有不正确的链接,这些链接在 _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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com