Allow defining default content negotiation strategy
During the HTTP Content Negotiation phase, the ContentNegotiationManager uses configured ContentNegotiationStrategy(ies) to define the list of content types accepted by the client. When HTTP clients don't send Accept headers, nor use a configured file extension in the request, nor a request param, developers can define a default content type using the ContentNegotiationConfigurer.defaultContentType() method. This change adds a new overloaded defaultContentType method that takes a ContentNegotiationStrategy as an argument. This strategy will take the current request as an argument and return a default content type. Issue: SPR-12286
This commit is contained in:
@@ -168,4 +168,17 @@ public class ContentNegotiationManagerFactoryBeanTests {
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
// SPR-12286
|
||||
@Test
|
||||
public void setDefaultContentTypeWithStrategy() throws Exception {
|
||||
this.factoryBean.setDefaultContentType(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
|
||||
this.factoryBean.afterPropertiesSet();
|
||||
ContentNegotiationManager manager = this.factoryBean.getObject();
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
|
||||
|
||||
this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user