io.github.novareseller.boot.config.ApiVersionHandlerMapping Maven / Gradle / Ivy
package io.github.novareseller.boot.config;
import io.github.novareseller.boot.annotation.ApiVersion;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
/**
* @author: Bowen huang
* @date: 2020/07/27
*/
public class ApiVersionHandlerMapping extends RequestMappingHandlerMapping {
@Override
protected boolean isHandler(Class> beanType) {
return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);
}
@Override
protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
Class> controllerClass = method.getDeclaringClass();
//类上的APIVersion注解
ApiVersion apiVersion = AnnotationUtils.findAnnotation(controllerClass, ApiVersion.class);
//方法上的APIVersion注解
ApiVersion methodAnnotation = AnnotationUtils.findAnnotation(method, ApiVersion.class);
//以方法上的注解优先
if (methodAnnotation != null) {
apiVersion = methodAnnotation;
}
String[] urlPatterns = apiVersion == null ? new String[0] : apiVersion.value();
PatternsRequestCondition apiPattern = new PatternsRequestCondition(urlPatterns);
PatternsRequestCondition oldPattern = mapping.getPatternsCondition();
PatternsRequestCondition updatedFinalPattern = oldPattern.combine(apiPattern);
//重新构建RequestMappingInfo
mapping = new RequestMappingInfo(mapping.getName(), updatedFinalPattern, mapping.getMethodsCondition(),
mapping.getParamsCondition(), mapping.getHeadersCondition(), mapping.getConsumesCondition(),
mapping.getProducesCondition(), mapping.getCustomCondition());
super.registerHandlerMethod(handler, method, mapping);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy