簡體   English   中英

如何在Spring Data REST中公開@EmbeddedId轉換器

[英]How to expose @EmbeddedId converters in 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:啟用了存儲庫,還有從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配置中,我在存儲庫中啟用了jpa:repositories和@RestResource

首先,您需要獲得可用的鏈接。 目前,您的復合ID公開為com.core.connection.domains.UserFriendshipId@5b10 應該足以覆蓋UserFriendshipIdtoString方法UserFriendshipId成像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配置,則可以按照文檔中的說明進行操作

為了擴展已接受的答案..當使用spring-boot時,為了使其工作,擴展RepositoryRestMvcConfiguration類也需要具有@Configuration注釋。 我還需要在彈簧啟動應用程序類中添加以下注釋:

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

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

我還在我的方法中調用了覆蓋configureConversionService的super方法:

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

這將保留默認轉換器,然后添加您的轉換器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM