All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iiifi.kite.boot.web.servlet.version.KiteRequestMappingHandlerMapping Maven / Gradle / Ivy

There is a newer version: 1.2.4.RELEASE
Show newest version
package com.iiifi.kite.boot.web.servlet.version;

import java.lang.reflect.Method;
import com.iiifi.kite.common.util.StringUtils;
import com.iiifi.kite.boot.annotation.ApiVersion;
import com.iiifi.kite.configuration.KiteProperties;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * url版本号处理 和 header 版本处理
 * @author [email protected] 花朝
 * @date 2018/8/18 13:46
 */
public class KiteRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

	private final KiteProperties properties;

	public KiteRequestMappingHandlerMapping(KiteProperties properties) {
		this.properties = properties;
	}

	@Override
	protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
		// 默认的规则
		RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
		if (mappingInfo == null) {
			return null;
		}

		// 版本号处理
		ApiVersion apiVersion = getApiVersionMappingInfo(method, handlerType);
		if (apiVersion == null) {
			return mappingInfo;
		}
		// 版本
		String version = apiVersion.value();
		if (StringUtils.isBlank(version)) {
			return mappingInfo;
		}

		RequestMappingInfo apiVersionMappingInfo = RequestMappingInfo
				.paths(version)
				.build();

		// 添加版本号
		mappingInfo = apiVersionMappingInfo.combine(mappingInfo);

		// 统一添加应用名,consumes
		if (apiVersion.useAppName()) {
			String applicationName = properties.getName();
			return RequestMappingInfo
					.paths(applicationName)
					.build()
					.combine(mappingInfo);
		}

		return mappingInfo;
	}

	private ApiVersion getApiVersionMappingInfo(Method method, Class handlerType) {
		// 优先获取方法上的版本
		ApiVersion apiVersion = AnnotatedElementUtils.findMergedAnnotation(method, ApiVersion.class);
		// 再次尝试类上的版本
		if (apiVersion == null || StringUtils.isBlank(apiVersion.value())) {
			apiVersion = AnnotatedElementUtils.findMergedAnnotation(handlerType, ApiVersion.class);
		}
		return apiVersion;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy