Response content type on REST API endpoints (controller classes or methods) in Spring Boot can be set using the @Produces annotation. Is there a way to set this application wide as a default for every endpoint in the REST API? For example, instead of writing @Produces("application/json") on every controller class or endpoint, can this be set on the entry application class? Or is there any other way to configure a default used until explicitely overwritten?
1
votes
1 Answers
1
votes
If you want to set the default Accept header, not of default "Content-Type" header, so this solution will only impact responses, not requests.
As of Spring Boot 2.x, you need to create a class that extends the WebMvcConfigurer interface, e.g.:
@Configuration
class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void configureContentNegotiation( ContentNegotiationConfigurer configurer){
configurer.defaultContentType( MediaType.APPLICATION_JSON );
}
}
Let me know the result. Good luck.