spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
915 stars 561 forks source link

Serializing and Deserializing json in a different format. [DATAREST-614] #987

Open spring-projects-issues opened 9 years ago

spring-projects-issues commented 9 years ago

Jack Matthews opened DATAREST-614 and commented

I need to serialize my json in http://jsonapi.org/ instead of HAL. I have tried modifying ObjectMapper by setting my own converters.

    @Bean
    public ConversionService conversionService() {
        ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
        bean.setConverters(getConverters());
        bean.afterPropertiesSet();
        ConversionService object = bean.getObject();
        return object;
    }

    private Set<Converter> getConverters() {
        Set<Converter> converters = new HashSet<>();

        converters.add(new JsonApiSerializingConverter());
        converters.add(new JsonApiDeserializingConverter());

        return converters;
    }

but this only modifies the "content" part of the payload. It still includes a whole lot of HAL boilerplate. Is there a way to register a custom json format and scrap the HAL one completely? I am happy to do the work in integrating this format but need some pointers in the best way to approach this


Affects: 2.1.6 (Dijkstra SR6)

1 votes, 3 watchers

spring-projects-issues commented 9 years ago

Oliver Drotbohm commented

A converter is not sufficient really as you need to take the Accept header and content negotiation into account. You could implement an HttpMessageConverter} (probably extending or configuring a {{MappingJackson2HttpMessageConverter) and register it on the configuration by overriding RepositoryRestMvcConfiguration.configureHttpMessageConverters(…)

spring-projects-issues commented 7 years ago

Will Oemler commented

Does the HttpMessageConverter have to be a subclass of MappingJackson2HttpMessageConverter? Could you simply extend AbstractHttpMessageConverter instead? I have a simple converter that writes to a tab-delimited text format, and works fine in vanilla Spring MVC, but not with Spring Data REST, even when registered in both RepositoryRestMvcConfiguration.configureHttpMessageConverters(…) and WebMvcConfigurerAdapter.configureMessageConverters(...)

spring-projects-issues commented 3 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.