How to expose @EmbeddedId converters in Spring Data REST

好久不见. 提交于 2019-11-28 07:48:57

At first you need to get a usable link. Currently your composite id is exposed as com.core.connection.domains.UserFriendshipId@5b10. It should be enough to override the toString method of UserFriendshipIdto produce something useful like 2-3.

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

class UserFriendShipIdConverter implements Converter<String, UserFriendshipId> {

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

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

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

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

To expand on the accepted answer.. when using spring-boot, in order to get this to work, my class that extends RepositoryRestMvcConfiguration also needed to 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 called the super method within my method that overrides configureConversionService:

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

This preserves the default converters, and then adds yours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!