Skip to content

Commit a011b36

Browse files
committed
Polish SPR-12286
Issue: SPR-12286
1 parent 2956049 commit a011b36

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public class ContentNegotiationManagerFactoryBean
6161

6262
private String parameterName = "format";
6363

64-
private MediaType defaultContentType;
65-
6664
private ContentNegotiationStrategy defaultNegotiationStrategy;
6765

6866
private ContentNegotiationManager contentNegotiationManager;
@@ -186,7 +184,7 @@ public void setIgnoreAcceptHeader(boolean ignoreAcceptHeader) {
186184
* determine the requested content type.
187185
*/
188186
public void setDefaultContentType(MediaType defaultContentType) {
189-
this.defaultContentType = defaultContentType;
187+
this.defaultNegotiationStrategy = new FixedContentNegotiationStrategy(defaultContentType);
190188
}
191189

192190
/**
@@ -196,7 +194,7 @@ public void setDefaultContentType(MediaType defaultContentType) {
196194
* the requested content type.
197195
* @since 4.1.2
198196
*/
199-
public void setDefaultContentType(ContentNegotiationStrategy defaultStrategy) {
197+
public void setDefaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
200198
this.defaultNegotiationStrategy = defaultStrategy;
201199
}
202200

@@ -235,10 +233,6 @@ public void afterPropertiesSet() {
235233
strategies.add(new HeaderContentNegotiationStrategy());
236234
}
237235

238-
if (this.defaultContentType != null) {
239-
strategies.add(new FixedContentNegotiationStrategy(this.defaultContentType));
240-
}
241-
242236
if(this.defaultNegotiationStrategy != null) {
243237
strategies.add(defaultNegotiationStrategy);
244238
}

spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void setDefaultContentType() throws Exception {
171171
// SPR-12286
172172
@Test
173173
public void setDefaultContentTypeWithStrategy() throws Exception {
174-
this.factoryBean.setDefaultContentType(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
174+
this.factoryBean.setDefaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
175175
this.factoryBean.afterPropertiesSet();
176176
ContentNegotiationManager manager = this.factoryBean.getObject();
177177

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeade
162162
* <p>This content type will be used when neither the request path extension,
163163
* nor a request parameter, nor the {@code Accept} header could help determine
164164
* the requested content type.
165+
* <p>Note that this method achieves the same goal as {@code defaultContentTypeStrategy}, so both
166+
* shouldn't be used at the same time.
167+
* @see #defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy)
165168
*/
166169
public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentType) {
167170
this.factoryBean.setDefaultContentType(defaultContentType);
@@ -173,10 +176,13 @@ public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentT
173176
* <p>This content type will be used when neither the request path extension,
174177
* nor a request parameter, nor the {@code Accept} header could help determine
175178
* the requested content type.
179+
* <p>Note that this method achieves the same goal as {@code defaultContentType}, so both
180+
* shouldn't be used at the same time.
176181
* @since 4.1.2
182+
* @see #defaultContentType(org.springframework.http.MediaType)
177183
*/
178-
public ContentNegotiationConfigurer defaultContentType(ContentNegotiationStrategy defaultStrategy) {
179-
this.factoryBean.setDefaultContentType(defaultStrategy);
184+
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
185+
this.factoryBean.setDefaultContentTypeStrategy(defaultStrategy);
180186
return this;
181187
}
182188

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public void setDefaultContentType() throws Exception {
113113
}
114114

115115
@Test
116-
public void setDefaultContentTypeWithStrategy() throws Exception {
117-
this.configurer.defaultContentType(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
116+
public void setDefaultContentTypeStrategy() throws Exception {
117+
this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
118118
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
119119

120120
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));

0 commit comments

Comments
 (0)