import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RequestMapping;
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.reflect.AnnotatedElement;
import java.lang.reflect.Method;
public class RequestMappingHandlerMappingWrapping extends RequestMappingHandlerMapping {
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class> handlerType) {
RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
Class> superClass = handlerType.getSuperclass();
mappingInfo = appendSuperRequestMapping(superClass,mappingInfo);
return mappingInfo;
}
protected RequestMappingInfo appendSuperRequestMapping(Class> handlerType,RequestMappingInfo mappingInfo) {
if(handlerType == null || mappingInfo == null) {
return mappingInfo;
}
RequestMapping superRequestMapping = handlerType.getAnnotation(RequestMapping.class);
if(superRequestMapping != null && superRequestMapping.value().length > 0) {
RequestMappingInfo typeInfo = this.createRequestMappingInfo(handlerType);
if (typeInfo != null) {
mappingInfo = typeInfo.combine(mappingInfo);
}
}
return appendSuperRequestMapping(handlerType.getSuperclass(),mappingInfo);
}
@Nullable
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
RequestMapping requestMapping = (RequestMapping) AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
RequestCondition> condition = element instanceof Class ? this.getCustomTypeCondition((Class)element) : this.getCustomMethodCondition((Method)element);
return requestMapping != null ? this.createRequestMappingInfo(requestMapping, condition) : null;
}
} import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebMvcConfiguration implements WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new RequestMappingHandlerMappingWrapping();
}
}