DATACMNS-885 - Support for binding JSON payloads to projection interfaces.

Projection types annotated with @ProjectedPayload can now be used as parameters for @RequestBody annotated Spring MVC controller method parameters.

Accessor methods will be translated into JSON path property expressions which can be customized by using the @JsonPath annotation. The methods are allowed to return simple types, nested projection interfaces or complex classes which will will be mapped using a Jackson ObjectMapper.
This commit is contained in:
Oliver Gierke
2016-07-17 12:32:59 +02:00
parent 86c1086976
commit eec2b43fd0
14 changed files with 952 additions and 78 deletions

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.projection;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
/**
* Helper value to abstract an accessor.
*
* @author Oliver Gierke
* @soundtrack Benny Greb - Soulfood (Live)
* @since 1.13
*/
public final class Accessor {
private final PropertyDescriptor descriptor;
private final Method method;
/**
* Creates an {@link Accessor} for the given {@link Method}.
*
* @param method must not be {@literal null}.
* @throws IllegalArgumentException in case the given method is not an accessor method.
*/
public Accessor(Method method) {
Assert.notNull(method, "Method must not be null!");
this.descriptor = BeanUtils.findPropertyForMethod(method);
this.method = method;
Assert.notNull(descriptor, String.format("Invoked method %s is no accessor method!", method));
}
/**
* Returns whether the accessor is a getter.
*
* @return
*/
public boolean isGetter() {
return method.equals(descriptor.getReadMethod());
}
/**
* Returns whether the accessor is a setter.
*
* @return
*/
public boolean isSetter() {
return method.equals(descriptor.getWriteMethod());
}
/**
* Returns the name of the property this accessor handles.
*
* @return will never be {@literal null}.
*/
public String getPropertyName() {
return descriptor.getName();
}
}

View File

@@ -15,14 +15,14 @@
*/
package org.springframework.data.projection;
import java.beans.PropertyDescriptor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.lang.reflect.Method;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
@@ -31,20 +31,10 @@ import org.springframework.util.ReflectionUtils;
* @author Oliver Gierke
* @since 1.10
*/
@RequiredArgsConstructor
class MapAccessingMethodInterceptor implements MethodInterceptor {
private final Map<String, Object> map;
/**
* Creates a new {@link MapAccessingMethodInterceptor} for the given {@link Map}.
*
* @param map must not be {@literal null}.
*/
public MapAccessingMethodInterceptor(Map<String, Object> map) {
Assert.notNull(map, "Map must not be null!");
this.map = map;
}
private final @NonNull Map<String, Object> map;
/*
* (non-Javadoc)
@@ -70,58 +60,4 @@ class MapAccessingMethodInterceptor implements MethodInterceptor {
throw new IllegalStateException("Should never get here!");
}
/**
* Helper value to abstract an accessor.
*
* @author Oliver Gierke
*/
private static final class Accessor {
private final PropertyDescriptor descriptor;
private final Method method;
/**
* Creates an {@link Accessor} for the given {@link Method}.
*
* @param method must not be {@literal null}.
* @throws IllegalArgumentException in case the given method is not an accessor method.
*/
public Accessor(Method method) {
Assert.notNull(method, "Method must not be null!");
this.descriptor = BeanUtils.findPropertyForMethod(method);
this.method = method;
Assert.notNull(descriptor, String.format("Invoked method %s is no accessor method!", method));
}
/**
* Returns whether the acessor is a getter.
*
* @return
*/
public boolean isGetter() {
return method.equals(descriptor.getReadMethod());
}
/**
* Returns whether the accessor is a setter.
*
* @return
*/
public boolean isSetter() {
return method.equals(descriptor.getWriteMethod());
}
/**
* Returns the name of the property this accessor handles.
*
* @return will never be {@literal null}.
*/
public String getPropertyName() {
return descriptor.getName();
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.projection;
import org.aopalliance.intercept.MethodInterceptor;
/**
* SPI to create {@link MethodInterceptor} instances based on the given source object and the target type to produce. To
* be registered with a {@link ProxyProjectionFactory} to customize the way method executions on projection proxies are
* handled.
*
* @author Oliver Gierke
* @see ProxyProjectionFactory
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
* @since 1.13
*/
public interface MethodInterceptorFactory {
/**
* Returns the {@link MethodInterceptor} to be used for the given source object and target type.
*
* @param source will never be {@literal null}.
* @param targetType will never be {@literal null}.
* @return
*/
MethodInterceptor createMethodInterceptor(Object source, Class<?> targetType);
/**
* Returns whether the current factory is supposed to be used to create a {@link MethodInterceptor} for proxy of the
* given target type.
*
* @param source will never be {@literal null}.
* @param targetType will never be {@literal null}.
* @return
*/
boolean supports(Object source, Class<?> targetType);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ public interface ProjectionFactory {
<T> T createProjection(Class<T> projectionType, Object source);
/**
* Creates a pojection instance for the given type.
* Creates a projection instance for the given type.
*
* @param projectionType the type to create, must not be {@literal null}.
* @return

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2105 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -47,8 +47,19 @@ class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware,
private static final boolean IS_JAVA_8 = org.springframework.util.ClassUtils.isPresent("java.util.Optional",
ProxyProjectionFactory.class.getClassLoader());
private final List<MethodInterceptorFactory> factories;
private ClassLoader classLoader;
/**
* Creates a new {@link ProxyProjectionFactory}.
*/
protected ProxyProjectionFactory() {
this.factories = new ArrayList<MethodInterceptorFactory>();
this.factories.add(MapAccessingMethodInterceptorFactory.INSTANCE);
this.factories.add(PropertyAccessingMethodInvokerFactory.INSTANCE);
}
/**
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
* @deprecated rather set the {@link ClassLoader} directly via {@link #setBeanClassLoader(ClassLoader)}.
@@ -68,6 +79,20 @@ class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware,
this.classLoader = classLoader;
}
/**
* Registers the given {@link MethodInterceptorFactory} to be used with the factory. Factories registered later enjoy
* precedence over previously registered ones.
*
* @param factory must not be {@literal null}.
* @since 1.13
*/
public void registerMethodInvokerFactory(MethodInterceptorFactory factory) {
Assert.notNull(factory, "MethodInterceptorFactory must not be null!");
this.factories.add(0, factory);
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.projection.ProjectionFactory#createProjection(java.lang.Object, java.lang.Class)
@@ -144,17 +169,33 @@ class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware,
* @param projectionType must not be {@literal null}.
* @return
*/
@SuppressWarnings("unchecked")
private MethodInterceptor getMethodInterceptor(Object source, Class<?> projectionType) {
MethodInterceptor propertyInvocationInterceptor = source instanceof Map
? new MapAccessingMethodInterceptor((Map<String, Object>) source)
: new PropertyAccessingMethodInterceptor(source);
MethodInterceptor propertyInvocationInterceptor = getFactoryFor(source, projectionType)
.createMethodInterceptor(source, projectionType);
return new ProjectingMethodInterceptor(this,
postProcessAccessorInterceptor(propertyInvocationInterceptor, source, projectionType));
}
/**
* Returns the {@link MethodInterceptorFactory} to be used with the given source object and target type.
*
* @param source must not be {@literal null}.
* @param projectionType must not be {@literal null}.
* @return
*/
private MethodInterceptorFactory getFactoryFor(Object source, Class<?> projectionType) {
for (MethodInterceptorFactory factory : factories) {
if (factory.supports(source, projectionType)) {
return factory;
}
}
throw new IllegalStateException("No MethodInterceptorFactory found for type ".concat(source.getClass().getName()));
}
/**
* Post-process the given {@link MethodInterceptor} for the given source instance and projection type. Default
* implementation will simply return the given interceptor.
@@ -218,4 +259,61 @@ class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware,
return invocation.proceed();
}
}
/**
* {@link MethodInterceptorFactory} handling {@link Map}s as target objects.
*
* @author Oliver Gierke
*/
private static enum MapAccessingMethodInterceptorFactory implements MethodInterceptorFactory {
INSTANCE;
/*
* (non-Javadoc)
* @see org.springframework.data.projection.MethodInterceptorFactory#createMethodInterceptor(java.lang.Object, java.lang.Class)
*/
@Override
@SuppressWarnings("unchecked")
public MethodInterceptor createMethodInterceptor(Object source, Class<?> targetType) {
return new MapAccessingMethodInterceptor((Map<String, Object>) source);
}
/*
* (non-Javadoc)
* @see org.springframework.data.projection.MethodInterceptorFactory#supports(java.lang.Object, java.lang.Class)
*/
@Override
public boolean supports(Object source, Class<?> targetType) {
return Map.class.isInstance(source);
}
}
/**
* {@link MethodInterceptorFactory} to create a {@link PropertyAccessingMethodInterceptor} for arbitrary objects.
*
* @author Oliver Gierke
*/
private static enum PropertyAccessingMethodInvokerFactory implements MethodInterceptorFactory {
INSTANCE;
/*
* (non-Javadoc)
* @see org.springframework.data.projection.MethodInterceptorFactory#createMethodInterceptor(java.lang.Object, java.lang.Class)
*/
@Override
public MethodInterceptor createMethodInterceptor(Object source, Class<?> targetType) {
return new PropertyAccessingMethodInterceptor(source);
}
/*
* (non-Javadoc)
* @see org.springframework.data.projection.MethodInterceptorFactory#supports(java.lang.Object, java.lang.Class)
*/
@Override
public boolean supports(Object source, Class<?> targetType) {
return true;
}
}
}