[英]How to pass @EmbeddedId in POST json in spring-boot data rest
[英]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
。 應該足以覆蓋UserFriendshipId
的toString
方法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());
}
為了擴展已接受的答案..當使用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.