How to expose @EmbeddedId converters in Spring Data REST

There are some objects with compound primary keys, and these objects, when opened, have invalid links that have the fully qualified class names in the URL inside _links

Also clicking on the links gives such errors -

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

I have a customized Spring XML repository with jpa enabled: repositories and repository distributed from JpaRepository

Can I make the repository implement org.springframework.core.convert.converter.converter.Converter to handle this. Currently getting the links below -

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

in xml config, I have jpa: repositories enabled and @RestResource enabled inside repositories

+6
source share
2 answers

First you need to get a useful link. Your composite identifier is currently displayed as com.core.connection.domains.UserFriendshipId@5b10 . This should be enough to override the toString UserFriendshipId method to create something useful, like 2-3 .

Then you need to implement the converter so that 2-3 can be converted back to UserFriendshipId :

 class UserFriendShipIdConverter implements Converter<String, UserFriendshipId> { UserFriendShipId convert(String id) { ... } } 

Finally, you need to register the converter. You already suggested overriding configureConversionService :

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

If you prefer XML configuration, you can follow the instructions in the documentation .

+3
source

To expand on the accepted answer .. when using spring-boot to make this work, my class that extends RepositoryRestMvcConfiguration should also have the @Configuration annotation. I also needed to add the following annotation to my spring boot application class:

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

I also named a super method in my method that overrides configureConversionService :

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

This saves the default converters and then adds your

+1
source

Source: https://habr.com/ru/post/976399/


All Articles