From a011b360d1f4735de45c9cbd4a063e1c4196c244 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 22 Oct 2014 21:15:34 +0200 Subject: [PATCH] Polish SPR-12286 Issue: SPR-12286 --- .../accept/ContentNegotiationManagerFactoryBean.java | 10 ++-------- .../ContentNegotiationManagerFactoryBeanTests.java | 2 +- .../annotation/ContentNegotiationConfigurer.java | 10 ++++++++-- .../annotation/ContentNegotiationConfigurerTests.java | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java index ead5b96baf..6b374a8d14 100644 --- a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java +++ b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java @@ -61,8 +61,6 @@ public class ContentNegotiationManagerFactoryBean private String parameterName = "format"; - private MediaType defaultContentType; - private ContentNegotiationStrategy defaultNegotiationStrategy; private ContentNegotiationManager contentNegotiationManager; @@ -186,7 +184,7 @@ public class ContentNegotiationManagerFactoryBean * determine the requested content type. */ public void setDefaultContentType(MediaType defaultContentType) { - this.defaultContentType = defaultContentType; + this.defaultNegotiationStrategy = new FixedContentNegotiationStrategy(defaultContentType); } /** @@ -196,7 +194,7 @@ public class ContentNegotiationManagerFactoryBean * the requested content type. * @since 4.1.2 */ - public void setDefaultContentType(ContentNegotiationStrategy defaultStrategy) { + public void setDefaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) { this.defaultNegotiationStrategy = defaultStrategy; } @@ -235,10 +233,6 @@ public class ContentNegotiationManagerFactoryBean strategies.add(new HeaderContentNegotiationStrategy()); } - if (this.defaultContentType != null) { - strategies.add(new FixedContentNegotiationStrategy(this.defaultContentType)); - } - if(this.defaultNegotiationStrategy != null) { strategies.add(defaultNegotiationStrategy); } diff --git a/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java b/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java index fc186a8d50..a093cf3140 100644 --- a/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java +++ b/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java @@ -171,7 +171,7 @@ public class ContentNegotiationManagerFactoryBeanTests { // SPR-12286 @Test public void setDefaultContentTypeWithStrategy() throws Exception { - this.factoryBean.setDefaultContentType(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)); + this.factoryBean.setDefaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java index 5499741c04..bec231223b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java @@ -162,6 +162,9 @@ public class ContentNegotiationConfigurer { *

This content type will be used when neither the request path extension, * nor a request parameter, nor the {@code Accept} header could help determine * the requested content type. + *

Note that this method achieves the same goal as {@code defaultContentTypeStrategy}, so both + * shouldn't be used at the same time. + * @see #defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy) */ public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentType) { this.factoryBean.setDefaultContentType(defaultContentType); @@ -173,10 +176,13 @@ public class ContentNegotiationConfigurer { *

This content type will be used when neither the request path extension, * nor a request parameter, nor the {@code Accept} header could help determine * the requested content type. + *

Note that this method achieves the same goal as {@code defaultContentType}, so both + * shouldn't be used at the same time. * @since 4.1.2 + * @see #defaultContentType(org.springframework.http.MediaType) */ - public ContentNegotiationConfigurer defaultContentType(ContentNegotiationStrategy defaultStrategy) { - this.factoryBean.setDefaultContentType(defaultStrategy); + public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) { + this.factoryBean.setDefaultContentTypeStrategy(defaultStrategy); return this; } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java index 665ae146da..902c09af89 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java @@ -113,8 +113,8 @@ public class ContentNegotiationConfigurerTests { } @Test - public void setDefaultContentTypeWithStrategy() throws Exception { - this.configurer.defaultContentType(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)); + public void setDefaultContentTypeStrategy() throws Exception { + this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)); ContentNegotiationManager manager = this.configurer.getContentNegotiationManager(); assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));