DATACMNS-867 - First draft.

This commit is contained in:
Oliver Gierke
2016-11-14 20:10:22 +01:00
parent 1b17271915
commit cc63e5b7a4
278 changed files with 4737 additions and 5714 deletions

View File

@@ -17,8 +17,12 @@ package org.springframework.data.web.config;
import java.util.List;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
import org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver;
import org.springframework.data.web.PagedResourcesAssembler;
@@ -36,6 +40,15 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@Configuration
public class HateoasAwareSpringDataWebConfiguration extends SpringDataWebConfiguration {
/**
* @param context must not be {@literal null}.
* @param conversionService must not be {@literal null}.
*/
public HateoasAwareSpringDataWebConfiguration(ApplicationContext context,
@Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService) {
super(context, conversionService);
}
/*
* (non-Javadoc)
* @see org.springframework.data.web.config.SpringDataWebConfiguration#pageableResolver()

View File

@@ -16,6 +16,7 @@
package org.springframework.data.web.config;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -54,7 +55,7 @@ public class QuerydslWebConfiguration extends WebMvcConfigurerAdapter {
@Lazy
@Bean
public QuerydslPredicateArgumentResolver querydslPredicateArgumentResolver() {
return new QuerydslPredicateArgumentResolver(querydslBindingsFactory(), conversionService.getObject());
return new QuerydslPredicateArgumentResolver(querydslBindingsFactory(), Optional.of(conversionService.getObject()));
}
@Lazy

View File

@@ -18,7 +18,6 @@ package org.springframework.data.web.config;
import java.util.List;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -52,8 +51,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public class SpringDataWebConfiguration extends WebMvcConfigurerAdapter {
@Autowired private ApplicationContext context;
@Autowired @Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService;
private final ApplicationContext context;
private final ObjectFactory<ConversionService> conversionService;
public SpringDataWebConfiguration(ApplicationContext context,
@Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService) {
this.context = context;
this.conversionService = conversionService;
}
/*
* (non-Javadoc)

View File

@@ -17,6 +17,7 @@ package org.springframework.data.web.querydsl;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Optional;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService;
@@ -56,11 +57,11 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
* @param factory
* @param conversionService defaults to {@link DefaultConversionService} if {@literal null}.
*/
public QuerydslPredicateArgumentResolver(QuerydslBindingsFactory factory, ConversionService conversionService) {
public QuerydslPredicateArgumentResolver(QuerydslBindingsFactory factory,
Optional<ConversionService> conversionService) {
this.bindingsFactory = factory;
this.predicateBuilder = new QuerydslPredicateBuilder(
conversionService == null ? new DefaultConversionService() : conversionService,
this.predicateBuilder = new QuerydslPredicateBuilder(conversionService.orElse(new DefaultConversionService()),
factory.getEntityPathResolver());
}
@@ -88,7 +89,7 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.method.support.ModelAndViewContainer, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.bind.support.WebDataBinderFactory)
*/
@Override
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public Predicate resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
@@ -98,12 +99,15 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
parameters.put(entry.getKey(), Arrays.asList(entry.getValue()));
}
QuerydslPredicate annotation = parameter.getParameterAnnotation(QuerydslPredicate.class);
Optional<QuerydslPredicate> annotation = Optional
.ofNullable(parameter.getParameterAnnotation(QuerydslPredicate.class));
TypeInformation<?> domainType = extractTypeInfo(parameter).getActualType();
Class<? extends QuerydslBinderCustomizer<?>> customizer = (Class<? extends QuerydslBinderCustomizer<?>>) (annotation == null
? null : annotation.bindings());
QuerydslBindings bindings = bindingsFactory.createBindingsFor(customizer, domainType);
Optional<? extends Class<? extends QuerydslBinderCustomizer>> map = annotation
.<Class<? extends QuerydslBinderCustomizer>> map(it -> it.bindings());
QuerydslBindings bindings = bindingsFactory.createBindingsFor(domainType,
(Optional<Class<? extends QuerydslBinderCustomizer<?>>>) map);
return predicateBuilder.getPredicate(domainType, parameters, bindings);
}
@@ -117,13 +121,12 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
*/
static TypeInformation<?> extractTypeInfo(MethodParameter parameter) {
QuerydslPredicate annotation = parameter.getParameterAnnotation(QuerydslPredicate.class);
Optional<QuerydslPredicate> annotation = Optional
.ofNullable(parameter.getParameterAnnotation(QuerydslPredicate.class));
if (annotation != null && !Object.class.equals(annotation.root())) {
return ClassTypeInformation.from(annotation.root());
}
return detectDomainType(ClassTypeInformation.fromReturnTypeOf(parameter.getMethod()));
return annotation.filter(it -> !Object.class.equals(it.root()))//
.<TypeInformation<?>> map(it -> ClassTypeInformation.from(it.root()))//
.orElseGet(() -> detectDomainType(ClassTypeInformation.fromReturnTypeOf(parameter.getMethod())));
}
private static TypeInformation<?> detectDomainType(TypeInformation<?> source) {