Created
June 5, 2016 14:12
-
-
Save ScienJus/e273bb7391665304f702e327ae82deb1 to your computer and use it in GitHub Desktop.
register @RequestMapping and overwrite path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scienjus; | |
import org.springframework.core.annotation.AnnotatedElementUtils; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.servlet.mvc.condition.RequestCondition; | |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.AnnotatedElement; | |
import java.lang.reflect.Method; | |
import java.util.Arrays; | |
/** | |
* @author ScienJus | |
* @date 16/6/5. | |
*/ | |
public class ApiHandlerMapping extends RequestMappingHandlerMapping { | |
/** | |
* Uses method and type-level @{@link RequestMapping} annotations to create | |
* the RequestMappingInfo. | |
* @return the created RequestMappingInfo, or {@code null} if the method | |
* does not have a {@code @RequestMapping} annotation. | |
* @see #getCustomMethodCondition(Method) | |
* @see #getCustomTypeCondition(Class) | |
*/ | |
@Override | |
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { | |
RequestMappingInfo info = createRequestMappingInfo(method); | |
if (info != null) { | |
RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType); | |
if (typeInfo != null) { | |
info = typeInfo.combine(info); | |
} | |
} | |
return info; | |
} | |
/** | |
* Delegates to {@link #createRequestMappingInfo(RequestMapping, RequestCondition)}, | |
* supplying the appropriate custom {@link RequestCondition} depending on whether | |
* the supplied {@code annotatedElement} is a class or method. | |
* @see #getCustomTypeCondition(Class) | |
* @see #getCustomMethodCondition(Method) | |
*/ | |
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) { | |
final RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class); | |
RequestMapping apiMapping = new RequestMapping() { | |
@Override | |
public boolean equals(Object obj) { | |
return requestMapping.equals(obj); | |
} | |
@Override | |
public int hashCode() { | |
return requestMapping.hashCode(); | |
} | |
@Override | |
public String toString() { | |
return requestMapping.toString(); | |
} | |
@Override | |
public Class<? extends Annotation> annotationType() { | |
return requestMapping.annotationType(); | |
} | |
@Override | |
public String name() { | |
return requestMapping.name(); | |
} | |
@Override | |
public String[] value() { | |
return requestMapping.value(); | |
} | |
@Override | |
public String[] path() { | |
return Arrays.stream(requestMapping.path()).map(path -> "/api" + path).toArray(String[]::new); | |
} | |
@Override | |
public RequestMethod[] method() { | |
return requestMapping.method(); | |
} | |
@Override | |
public String[] params() { | |
return requestMapping.params(); | |
} | |
@Override | |
public String[] headers() { | |
return requestMapping.headers(); | |
} | |
@Override | |
public String[] consumes() { | |
return requestMapping.consumes(); | |
} | |
@Override | |
public String[] produces() { | |
return requestMapping.produces(); | |
} | |
}; | |
RequestCondition<?> condition = (element instanceof Class<?> ? | |
getCustomTypeCondition((Class<?>) element) : getCustomMethodCondition((Method) element)); | |
return (requestMapping != null ? createRequestMappingInfo(apiMapping, condition) : null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment