|
|
@ -32,6 +32,7 @@ import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.factory.BeanFactory;
|
|
|
|
import org.springframework.beans.factory.BeanFactory;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanFactoryAware;
|
|
|
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
|
|
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
@ -39,11 +40,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
|
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
|
|
@ -72,14 +69,14 @@ import org.springframework.context.annotation.Conditional;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.ConfigurationCondition;
|
|
|
|
import org.springframework.context.annotation.ConfigurationCondition;
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
import org.springframework.context.annotation.Role;
|
|
|
|
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
|
|
|
|
import org.springframework.core.convert.converter.Converter;
|
|
|
|
import org.springframework.core.convert.converter.Converter;
|
|
|
|
import org.springframework.core.convert.converter.GenericConverter;
|
|
|
|
import org.springframework.core.convert.converter.GenericConverter;
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
|
|
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
|
|
|
|
|
|
|
import org.springframework.core.type.AnnotationMetadata;
|
|
|
|
import org.springframework.format.Formatter;
|
|
|
|
import org.springframework.format.Formatter;
|
|
|
|
import org.springframework.format.FormatterRegistry;
|
|
|
|
import org.springframework.format.FormatterRegistry;
|
|
|
|
import org.springframework.format.datetime.DateFormatter;
|
|
|
|
import org.springframework.format.datetime.DateFormatter;
|
|
|
@ -160,19 +157,6 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
public static final String DEFAULT_SUFFIX = "";
|
|
|
|
public static final String DEFAULT_SUFFIX = "";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Attribute that can be added to the web request when the
|
|
|
|
|
|
|
|
* {@link PathExtensionContentNegotiationStrategy} should be be skipped.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static final String SKIP_PATH_EXTENSION_CONTENT_NEGOTIATION_ATTRIBUTE = PathExtensionContentNegotiationStrategy.class
|
|
|
|
|
|
|
|
.getName() + ".SKIP";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
|
|
|
|
|
|
|
public static MvcValidatorPostProcessor mvcValidatorAliasPostProcessor() {
|
|
|
|
|
|
|
|
return new MvcValidatorPostProcessor();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
|
|
|
|
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
|
|
|
|
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
|
|
|
|
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
|
|
|
@ -189,7 +173,7 @@ public class WebMvcAutoConfiguration {
|
|
|
|
// Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when not
|
|
|
|
// Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when not
|
|
|
|
// on the classpath
|
|
|
|
// on the classpath
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
@Import(EnableWebMvcConfiguration.class)
|
|
|
|
@Import({ EnableWebMvcConfiguration.class, MvcValidatorRegistrar.class })
|
|
|
|
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
|
|
|
|
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
|
|
|
|
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
|
|
|
|
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
|
|
|
|
|
|
|
|
|
|
|
@ -623,6 +607,9 @@ public class WebMvcAutoConfiguration {
|
|
|
|
static class OptionalPathExtensionContentNegotiationStrategy
|
|
|
|
static class OptionalPathExtensionContentNegotiationStrategy
|
|
|
|
implements ContentNegotiationStrategy {
|
|
|
|
implements ContentNegotiationStrategy {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String SKIP_ATTRIBUTE = PathExtensionContentNegotiationStrategy.class
|
|
|
|
|
|
|
|
.getName() + ".SKIP";
|
|
|
|
|
|
|
|
|
|
|
|
private final ContentNegotiationStrategy delegate;
|
|
|
|
private final ContentNegotiationStrategy delegate;
|
|
|
|
|
|
|
|
|
|
|
|
OptionalPathExtensionContentNegotiationStrategy(
|
|
|
|
OptionalPathExtensionContentNegotiationStrategy(
|
|
|
@ -633,8 +620,7 @@ public class WebMvcAutoConfiguration {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
|
|
|
|
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
|
|
|
|
throws HttpMediaTypeNotAcceptableException {
|
|
|
|
throws HttpMediaTypeNotAcceptableException {
|
|
|
|
Object skip = webRequest.getAttribute(
|
|
|
|
Object skip = webRequest.getAttribute(SKIP_ATTRIBUTE,
|
|
|
|
SKIP_PATH_EXTENSION_CONTENT_NEGOTIATION_ATTRIBUTE,
|
|
|
|
|
|
|
|
RequestAttributes.SCOPE_REQUEST);
|
|
|
|
RequestAttributes.SCOPE_REQUEST);
|
|
|
|
if (skip != null && Boolean.parseBoolean(skip.toString())) {
|
|
|
|
if (skip != null && Boolean.parseBoolean(skip.toString())) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
return Collections.emptyList();
|
|
|
@ -646,7 +632,7 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Condition used to disable the default MVC validator registration. The
|
|
|
|
* Condition used to disable the default MVC validator registration. The
|
|
|
|
* {@link MvcValidatorPostProcessor} is used to configure the {@code mvcValidator}
|
|
|
|
* {@link MvcValidatorRegistrar} is actually used to register the {@code mvcValidator}
|
|
|
|
* bean.
|
|
|
|
* bean.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static class DisableMvcValidatorCondition implements ConfigurationCondition {
|
|
|
|
static class DisableMvcValidatorCondition implements ConfigurationCondition {
|
|
|
@ -664,8 +650,8 @@ public class WebMvcAutoConfiguration {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* {@link BeanFactoryPostProcessor} to deal with the MVC validator bean registration.
|
|
|
|
* {@link ImportBeanDefinitionRegistrar} to deal with the MVC validator bean
|
|
|
|
* Applies the following rules:
|
|
|
|
* registration. Applies the following rules:
|
|
|
|
* <ul>
|
|
|
|
* <ul>
|
|
|
|
* <li>With no validators - Uses standard
|
|
|
|
* <li>With no validators - Uses standard
|
|
|
|
* {@link WebMvcConfigurationSupport#mvcValidator()} logic.</li>
|
|
|
|
* {@link WebMvcConfigurationSupport#mvcValidator()} logic.</li>
|
|
|
@ -674,43 +660,45 @@ public class WebMvcAutoConfiguration {
|
|
|
|
* defined.</li>
|
|
|
|
* defined.</li>
|
|
|
|
* </ul>
|
|
|
|
* </ul>
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Order(Ordered.LOWEST_PRECEDENCE)
|
|
|
|
static class MvcValidatorRegistrar
|
|
|
|
static class MvcValidatorPostProcessor
|
|
|
|
implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
|
|
|
|
implements BeanDefinitionRegistryPostProcessor {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String JSR303_VALIDATOR_CLASS = "javax.validation.Validator";
|
|
|
|
private static final String JSR303_VALIDATOR_CLASS = "javax.validation.Validator";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private BeanFactory beanFactory;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
|
|
|
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
|
|
throws BeansException {
|
|
|
|
this.beanFactory = beanFactory;
|
|
|
|
if (registry instanceof ListableBeanFactory) {
|
|
|
|
|
|
|
|
postProcess(registry, (ListableBeanFactory) registry);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
|
|
|
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
|
|
|
throws BeansException {
|
|
|
|
BeanDefinitionRegistry registry) {
|
|
|
|
|
|
|
|
if (this.beanFactory instanceof ListableBeanFactory) {
|
|
|
|
|
|
|
|
registerOrAliasMvcValidator(registry,
|
|
|
|
|
|
|
|
(ListableBeanFactory) this.beanFactory);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void postProcess(BeanDefinitionRegistry registry,
|
|
|
|
private void registerOrAliasMvcValidator(BeanDefinitionRegistry registry,
|
|
|
|
ListableBeanFactory beanFactory) {
|
|
|
|
ListableBeanFactory beanFactory) {
|
|
|
|
String[] validatorBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
|
|
|
String[] validatorBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
|
|
|
beanFactory, Validator.class, false, false);
|
|
|
|
beanFactory, Validator.class, false, false);
|
|
|
|
if (validatorBeans.length == 0) {
|
|
|
|
if (validatorBeans.length == 0) {
|
|
|
|
registerMvcValidator(registry, beanFactory);
|
|
|
|
registerNewMvcValidator(registry, beanFactory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (validatorBeans.length == 1) {
|
|
|
|
else if (validatorBeans.length == 1) {
|
|
|
|
registry.registerAlias(validatorBeans[0], "mvcValidator");
|
|
|
|
registry.registerAlias(validatorBeans[0], "mvcValidator");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
if (!ObjectUtils.containsElement(validatorBeans, "mvcValidator")) {
|
|
|
|
if (!ObjectUtils.containsElement(validatorBeans, "mvcValidator")) {
|
|
|
|
registerMvcValidator(registry, beanFactory);
|
|
|
|
registerNewMvcValidator(registry, beanFactory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void registerMvcValidator(BeanDefinitionRegistry registry,
|
|
|
|
private void registerNewMvcValidator(BeanDefinitionRegistry registry,
|
|
|
|
ListableBeanFactory beanFactory) {
|
|
|
|
ListableBeanFactory beanFactory) {
|
|
|
|
RootBeanDefinition definition = new RootBeanDefinition();
|
|
|
|
RootBeanDefinition definition = new RootBeanDefinition();
|
|
|
|
definition.setBeanClass(getClass());
|
|
|
|
definition.setBeanClass(getClass());
|
|
|
|