Spring Data Rest: custom Converter<Entity, Resource> is never invoked

让人想犯罪 __ 提交于 2019-12-23 02:07:23

问题


I'm trying to implement a custom Converter for an Entity to a Resource object with Spring Data Rest, but the Converter is never invoked.

I'm following this documentation:

If your project needs to have output in a different format, however, it’s possible to completely replace the default outgoing JSON representation with your own. If you register your own ConversionService in the ApplicationContext and register your own Converter, then you can return a Resource implementation of your choosing.

That's what I've tried to do.

I have a @Configuration class that extends RepositoryRestMvcConfiguration, with this method:

@Configuration
@EnableWebMvc
@EnableHypermediaSupport(type = HypermediaType.HAL)
public class RepositoryBaseConfiguration extends RepositoryRestMvcConfiguration {

    @Override
    public DefaultFormattingConversionService defaultConversionService() {
        return super.defaultConversionService();
    }

}

And I have a Class that extends RepositoryRestConfigurerAdapter, with this implementation:

@Configuration
public class RepositoryBaseConfigurerAdapter extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureConversionService(ConfigurableConversionService conversionService) {
        if(!conversionService.canConvert(Entity.class, Resource.class))
            conversionService.addConverter(new EntityConverter());
        super.configureConversionService(conversionService);
    }

}

Both methods of those two classes are correctly invoked and managed, so it's natural to think that the Converter has been registered in the Application Context...

This is my custom converter EntityConverter:

@Component
public class EntityConverter implements Converter<Entity, Resource> {

    @Override
    public Resource convert(Entity source) {
        System.out.println("method convert of class EntityConverter");
        return null;
    }

}

The method "convert" is never invoked by Spring Data Rest.

What's wrong/missing ?

Thanks in advance.

来源:https://stackoverflow.com/questions/38591987/spring-data-rest-custom-converterentity-resource-is-never-invoked

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