问题
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