Work around Servlet dependency in content negotiation

Before this change the PathExtensionContentNegotiationStrategy accessed
the ServletContext via request.getServletContext, which is Servlet 3
specific. To work around it, there is now a Servlet-specific sub-class
that accepts a ServletContext as a constructor argument.

The ContentNegotiationManagerFactoryBean is now ServletContextAware and
if it has a ServletContext it creates the Servlet-specific sub-class
of PathExtensionContentNegotiationStrategy.

The ContentNegotiationManagerFactoryBean is now also used in several
places internally -- MVC namespace, MVC Java config, and the
ContentNegotiatingViewResolver -- to reduce duplication.

Issue: SPR-9826
This commit is contained in:
Rossen Stoyanchev
2012-09-26 09:16:08 -04:00
parent 2bb0104556
commit 7b30ffd522
11 changed files with 227 additions and 187 deletions

View File

@@ -42,9 +42,9 @@ public class ContentNegotiationConfigurerTests {
@Before
public void setup() {
this.configurer = new ContentNegotiationConfigurer();
this.servletRequest = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(this.servletRequest);
this.configurer = new ContentNegotiationConfigurer(this.servletRequest.getServletContext());
}
@Test
@@ -71,7 +71,7 @@ public class ContentNegotiationConfigurerTests {
@Test
public void addMediaTypes() throws Exception {
this.configurer.addMediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
this.servletRequest.setRequestURI("/flower.json");
@@ -80,9 +80,9 @@ public class ContentNegotiationConfigurerTests {
@Test
public void favorParameter() throws Exception {
this.configurer.setFavorParameter(true);
this.configurer.setParameterName("f");
this.configurer.addMediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
this.configurer.favorParameter(true);
this.configurer.parameterName("f");
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
this.servletRequest.setRequestURI("/flower");
@@ -93,7 +93,7 @@ public class ContentNegotiationConfigurerTests {
@Test
public void ignoreAcceptHeader() throws Exception {
this.configurer.setIgnoreAcceptHeader(true);
this.configurer.ignoreAcceptHeader(true);
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
this.servletRequest.setRequestURI("/flower");
@@ -104,7 +104,7 @@ public class ContentNegotiationConfigurerTests {
@Test
public void setDefaultContentType() throws Exception {
this.configurer.setDefaultContentType(MediaType.APPLICATION_JSON);
this.configurer.defaultContentType(MediaType.APPLICATION_JSON);
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));

View File

@@ -235,7 +235,7 @@ public class WebMvcConfigurationSupportExtensionTests {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.setFavorParameter(true).setParameterName("f");
configurer.favorParameter(true).parameterName("f");
}
@Override