Java – how to set the default media type for spring data rest?
From the repository restconfiguration, I can see setting up spring data. rest. Default media type = Application / JSON you can change the default media type provided by @ repositoryrestresource
@SuppressWarnings("deprecation") public class RepositoryRestConfiguration { private MediaType defaultMediaType = MediaTypes.HAL_JSON; }
Question: since this class is deprecated, what is the correct way to set / override the default type?
Solution
You can use repository restconfiguration or just application Properties to perform this operation See the documentation here
The repositoryrestconfiguration class is not recommended Some of these methods have been deprecated The @ suppresswarnings ("deprecation") annotation on a class does not mean that the class itself has been deprecated This is just a comment to tell the IDE not to display a deprecation warning in the IDE
The simplest way is in application Properties However, your attribute name is incorrect You will not set it to spring data. rest. default-media-type. The actual attribute it expects is spring data. rest. defaultMediaType. So in your application In properties, you can:
spring.data.rest.defaultMediaType=application/json
Using repository restconfiguration, you can do the same:
@Configuration class CustomRestMvcConfiguration { @Bean public RepositoryRestConfigurer repositoryRestConfigurer() { return new RepositoryRestConfigurerAdapter() { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.setDefaultMediaType(MediaType.APPLICATION_JSON); } }; } }