Search code examples
springspring-mvcspring-boothttp-accept-header

Spring Boot: Add new Extension-AcceptHeader mappings


Within a normal Spring Application, I have:

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected Map<String, MediaType> getDefaultMediaTypes() {
        Map<String, MediaType> mediaTypes = super.getDefaultMediaTypes();
        mediaTypes.put("extension", new MediaType("foo", "bar"));
        return mediaTypes;
    }
}

So I can do something like:

@RequestMapping(produces = "foo/bar")
public void test() { ... }

And then call:

http://.../myResource.extension

When I do this with Spring Boot, then extends WebMvcConfigurationSupport would prevent all the auto configuration.

So how can I easily register new Extension-Accept-Header mappings with Spring Boot?


Solution

  • This should do it, I have verified the code with Boot 1.2.1.RELEASE

    @Configuration
     public class EnableWebMvcConfiguration extends WebMvcAutoConfiguration.EnableWebMvcConfiguration {
    
        @Override
        protected Map<String, MediaType> getDefaultMediaTypes() {
            Map<String, MediaType> mediaTypes = super.getDefaultMediaTypes();
            mediaTypes.put("extension", new MediaType("foo", "bar"));
            return mediaTypes;
        }
    }