Spring Data Rest: how to register a custom conversion service with a custom converter <Entity, Resource>?
In this part of the Spring Data Storage Documentation:
The Spring Data REST exporter executes any ResourceProcessor `detected before creating the output view.
What I noticed is true: ResourceProcessor is called during request processing after completion of the corresponding RepositoryEntityController method.
He does this by registering an instance
Converter<Entity, Resource>with the internal ConversionService service.
I do not understand when it is used Converter<Entity,Resource>.
This is the component responsible for creating links to object links (for example, those objects that are in the _links object in JSON objects). It takes @Entity and iterates over the property, creating links for those properties that are managed by the Repository and copying any built-in or simple properties.
Sure? I noticed that _links for link objects are created in RepositoryEntityController. I have not seen another component that builds these links: there is no ConversionService or Converter.
If your project should have output in a different format, it can be completely replaced by the outgoing JSON default view with your own. If you register your own ConversionService in ApplicationContext and register your own Converter, then you can return the resource of your choice.
, .
, : ConversionService ApplicationContext .
ConversionService , RepositoryRestMvcConfiguration:
@Configuration
public class RepositoryConfiguration extends RepositoryRestMvcConfiguration {
@Autowired
AuthorConverter authorConverter;
@Bean(name="conversionService")
public ConversionService getConversionService() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
conversionService.addConverter(authorConverter);
return conversionService;
}
@Override
public DefaultFormattingConversionService defaultConversionService() {
return (DefaultFormattingConversionService) getConversionService();
}
}
AuthorConverter:
@Component
public class AuthorConverter implements Converter<Author, Resource> {
@Override
public Resource convert(Author source) {
System.out.println("convert method of class AuthorConverter");
// still to be implemented
return null;
}
}
: URL- /authors, JSON , .
(, ), , .
.
? : http://www.baeldung.com/spring-httpmessageconverter-rest
" , WebMvcConfigurerAdapter configureMessageConverters:
@EnableWebMvc
@Configuration
@ComponentScan({ "org.baeldung.web" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
messageConverters.add(createXmlHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
MarshallingHttpMessageConverter xmlConverter =
new MarshallingHttpMessageConverter();
XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
xmlConverter.setMarshaller(xstreamMarshaller);
xmlConverter.setUnmarshaller(xstreamMarshaller);
return xmlConverter;
}
}