#361 - Rearrange PropertyResolvingMappingDiscoverer.

Moved PropertyResolvingMappingDiscoverer into server.core package. Unified MappingDiscoverer arrangement in WebHandler and use it from there. Deprecated AnnotationBasedMappingDiscoverer to be able to make it package protected in 1.3.
This commit is contained in:
Oliver Drotbohm
2020-07-27 18:16:46 +02:00
parent ffa1dc3539
commit 5ea48dd332
5 changed files with 46 additions and 54 deletions

View File

@@ -33,11 +33,13 @@ import org.springframework.web.bind.annotation.RequestMethod;
/**
* {@link MappingDiscoverer} implementation that inspects mappings from a particular annotation.
*
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Greg Turnquist
* @deprecated since 1.2, not for removal but for hiding within the package in 1.3
*/
@Deprecated
public class AnnotationMappingDiscoverer implements MappingDiscoverer {
private static final Pattern MULTIPLE_SLASHES = Pattern.compile("/{2,}");
@@ -48,7 +50,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
/**
* Creates an {@link AnnotationMappingDiscoverer} for the given annotation type. Will lookup the {@code value}
* attribute by default.
*
*
* @param annotation must not be {@literal null}.
*/
public AnnotationMappingDiscoverer(Class<? extends Annotation> annotation) {
@@ -57,7 +59,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
/**
* Creates an {@link AnnotationMappingDiscoverer} for the given annotation type and attribute name.
*
*
* @param annotation must not be {@literal null}.
* @param mappingAttributeName if {@literal null}, it defaults to {@code value}.
*/
@@ -69,7 +71,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
this.mappingAttributeName = mappingAttributeName;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class)
*/
@@ -84,7 +86,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
return mapping.length == 0 ? null : mapping[0];
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.reflect.Method)
*/
@@ -96,7 +98,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
return getMapping(method.getDeclaringClass(), method);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class, java.lang.reflect.Method)
*/
@@ -120,7 +122,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
/**
* Extract {@link org.springframework.web.bind.annotation.RequestMapping}'s list of {@link RequestMethod}s into an
* array of {@link String}s.
*
*
* @param type
* @param method
* @return
@@ -171,7 +173,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
/**
* Joins the given mappings making sure exactly one slash.
*
*
* @param typeMapping must not be {@literal null} or empty.
* @param mapping must not be {@literal null} or empty.
* @return

View File

@@ -13,12 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.server.mvc;
package org.springframework.hateoas.server.core;
import java.lang.reflect.Method;
import java.util.Collection;
import org.springframework.hateoas.server.core.MappingDiscoverer;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

View File

@@ -22,16 +22,7 @@ import static org.springframework.web.util.UriComponents.UriTemplateVariables.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Function;
@@ -68,8 +59,9 @@ import org.springframework.web.util.UriTemplate;
*/
public class WebHandler {
private static final MappingDiscoverer DISCOVERER = CachingMappingDiscoverer
.of(new AnnotationMappingDiscoverer(RequestMapping.class));
@SuppressWarnings("deprecation") //
public static final MappingDiscoverer DISCOVERER = CachingMappingDiscoverer
.of(new PropertyResolvingMappingDiscoverer(new AnnotationMappingDiscoverer(RequestMapping.class)));
private static final Map<AffordanceKey, List<Affordance>> AFFORDANCES_CACHE = new ConcurrentReferenceHashMap<>();
@@ -169,7 +161,7 @@ public class WebHandler {
}
List<Affordance> affordances = AFFORDANCES_CACHE.computeIfAbsent(
AffordanceKey.of(invocation.getTargetType(), invocation.getMethod(), components),
new AffordanceKey(invocation.getTargetType(), invocation.getMethod(), components),
key -> SpringAffordanceBuilder.create(key.type, key.method, key.href.toUriString(), DISCOVERER));
return creator.createBuilder(components, variables, affordances);
@@ -242,46 +234,49 @@ public class WebHandler {
private final Method method;
private final UriComponents href;
private AffordanceKey(Class<?> type, Method method, UriComponents href) {
AffordanceKey(Class<?> type, Method method, UriComponents href) {
this.type = type;
this.method = method;
this.href = href;
}
public static AffordanceKey of(Class<?> type, Method method, UriComponents href) {
return new AffordanceKey(type, method, href);
}
public Class<?> getType() {
return this.type;
}
public Method getMethod() {
return this.method;
}
public UriComponents getHref() {
return this.href;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o)
if (this == o) {
return true;
if (!(o instanceof AffordanceKey))
}
if (!(o instanceof AffordanceKey)) {
return false;
}
AffordanceKey that = (AffordanceKey) o;
return Objects.equals(this.type, that.type) && Objects.equals(this.method, that.method)
return Objects.equals(this.type, that.type) //
&& Objects.equals(this.method, that.method) //
&& Objects.equals(this.href, that.href);
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hash(this.type, this.method, this.href);
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "WebHandler.AffordanceKey(type=" + this.type + ", method=" + this.method + ", href=" + this.href + ")";
}

View File

@@ -24,14 +24,11 @@ import java.util.Map;
import org.springframework.hateoas.Affordance;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.TemplateVariables;
import org.springframework.hateoas.server.core.AnnotationMappingDiscoverer;
import org.springframework.hateoas.server.core.CachingMappingDiscoverer;
import org.springframework.hateoas.server.core.DummyInvocationUtils;
import org.springframework.hateoas.server.core.MappingDiscoverer;
import org.springframework.hateoas.server.core.TemplateVariableAwareLinkBuilderSupport;
import org.springframework.hateoas.server.core.UriTemplateFactory;
import org.springframework.hateoas.server.core.WebHandler;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.DefaultUriTemplateHandler;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -52,8 +49,6 @@ import org.springframework.web.util.UriTemplate;
@SuppressWarnings("deprecation")
public class WebMvcLinkBuilder extends TemplateVariableAwareLinkBuilderSupport<WebMvcLinkBuilder> {
private static final MappingDiscoverer DISCOVERER = CachingMappingDiscoverer
.of(new PropertyResolvingMappingDiscoverer(new AnnotationMappingDiscoverer(RequestMapping.class)));
private static final WebMvcLinkBuilderFactory FACTORY = new WebMvcLinkBuilderFactory();
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler();
@@ -94,7 +89,7 @@ public class WebMvcLinkBuilder extends TemplateVariableAwareLinkBuilderSupport<W
Assert.notNull(controller, "Controller must not be null!");
Assert.notNull(parameters, "Parameters must not be null!");
String mapping = DISCOVERER.getMapping(controller);
String mapping = WebHandler.DISCOVERER.getMapping(controller);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -116,7 +111,7 @@ public class WebMvcLinkBuilder extends TemplateVariableAwareLinkBuilderSupport<W
Assert.notNull(controller, "Controller must not be null!");
Assert.notNull(parameters, "Parameters must not be null!");
String mapping = DISCOVERER.getMapping(controller);
String mapping = WebHandler.DISCOVERER.getMapping(controller);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -139,7 +134,7 @@ public class WebMvcLinkBuilder extends TemplateVariableAwareLinkBuilderSupport<W
Assert.notNull(controller, "Controller type must not be null!");
Assert.notNull(method, "Method must not be null!");
String mapping = DISCOVERER.getMapping(controller, method);
String mapping = WebHandler.DISCOVERER.getMapping(controller, method);
UriTemplate template = UriTemplateFactory.templateFor(mapping);
URI uri = template.expand(parameters);