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:
@@ -63,6 +63,8 @@ public class ContentNegotiationManagerFactoryBean
|
||||
|
||||
private MediaType defaultContentType;
|
||||
|
||||
private ContentNegotiationStrategy defaultNegotiationStrategy;
|
||||
|
||||
private ContentNegotiationManager contentNegotiationManager;
|
||||
|
||||
private ServletContext servletContext;
|
||||
@@ -187,6 +189,17 @@ public class ContentNegotiationManagerFactoryBean
|
||||
this.defaultContentType = defaultContentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link ContentNegotiationStrategy} to be used to resolving the default content type.
|
||||
* <p>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.
|
||||
* @since 4.1.2
|
||||
*/
|
||||
public void setDefaultContentType(ContentNegotiationStrategy defaultStrategy) {
|
||||
this.defaultNegotiationStrategy = defaultStrategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServletContext(ServletContext servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
@@ -226,6 +239,10 @@ public class ContentNegotiationManagerFactoryBean
|
||||
strategies.add(new FixedContentNegotiationStrategy(this.defaultContentType));
|
||||
}
|
||||
|
||||
if(this.defaultNegotiationStrategy != null) {
|
||||
strategies.add(defaultNegotiationStrategy);
|
||||
}
|
||||
|
||||
this.contentNegotiationManager = new ContentNegotiationManager(strategies);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user