当前位置: 首页 > 知识库问答 >
问题:

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

浦修文
2023-03-14

有一些实体具有复合主键,这些实体在公开时有不正确的链接,在URL内有类的完整限定名

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

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

我在Spring Repository中配置了XML,启用了jpa:repositories,并从JpaRepository扩展了Respository

我可以让存储库实现org吗。springframework。果心转换转换器。转换器来处理这个问题。目前正在获取以下链接-

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

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

共有2个答案

阮轶
2023-03-14

扩展已被接受的答案。。在使用spring boot时,为了使其正常工作,扩展了RepositoryRestMvcConfiguration的类还需要有@Configuration注释。我还需要在spring boot应用程序类中添加以下注释:

@SpringBootApplication
@Import(MyConfiguration.class)
public class ApplicationContext {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationContext.class,args);
    }
}

我还调用了方法中覆盖configureConversionService的超级方法:

    @Override
    protected void configureConversionService(ConfigurableConversionService conversionService) {
        super.configureConversionService(conversionService);
        conversionService.addConverter(new TaskPlatformIdConverter());
    }

这将保留默认转换器,然后添加您的转换器

汪凌
2023-03-14

首先,你需要获得一个可用的链接。目前,您的复合id公开为com。果心联系域名。UserFriendshipId@5b10。它应该足以覆盖UserFriendshipIdtoString方法,从而生成像2-3这样有用的东西。

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

class UserFriendShipIdConverter implements Converter<String, UserFriendshipId> {

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

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

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

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

 类似资料:
  • 有一些实体具有复合主键,这些实体在公开时有不正确的链接,在URL内有类的完整限定名 点击链接也会出现这样的错误- 我在Spring Repository中配置了XML,启用了jpa:repositories,并从JpaRepository扩展了Respository 我可以让存储库实现org吗。springframework。果心转换转换器。转换器来处理这个问题。目前正在获取以下链接- 在xml配

  • 我不想公开我的模型类(jpa实体),而是用不同的数据传输对象(DTO)公开它们属性的不同子集。这个想法是不切实际的 例子: 实体: JpaRepository: DTO: D到积垢积存物: 实施: 一般: 在本例中,我想用Spring数据REST公开GroupDtoDao。 在其他bean中,我可以自动连接GroupDto和GroupDto,所以两者都由Spring的上下文管理。如果我不使用@Re

  • 我有以下公钥(作为示例): 我需要能够将其转换为可以插入ssh known_hosts文件的格式。例如: 谢了!

  • 问题内容: 正如一位评论者所述,您必须将以km为单位的半径转换为度数…但是如何?我想这是个快速公式,但对我来说却是希腊语。我将不胜感激任何指针。 问题答案: 答案可能取决于地球上的位置。在赤道附近,1km大约等于纬度和经度的0.008度(1 km / 40,000 km * 360度),但是在极点附近,1km大约等于纬度的0.008度,但是可能是许多度的经度。(如果您距离极点1公里,则向西行驶1公

  • 问题内容: 我是Java的新手,想知道double转换为int cast如何工作吗?我知道,只需将低32位转换为int就很容易了,但是将double(64位)转换为int(32位)呢?来自二进制的double中的那64位是Double- precision浮点格式(Mantissa),那么它如何在内部转换为int? 问题答案: 所有这些都记录在JLS的5.1.3节中。 第一步,将浮点数转换为lon

  • 我正试图从JSONB转换为Google GSON JSONElement。我使用的转换器来自:https://www.jooq.org/doc/3.11/manual/code-generation/custom-data-type-bindings/ TableImpl中的列: