前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot3配置Mapping统一前缀

Springboot3配置Mapping统一前缀

作者头像
尚浩宇
发布2023-05-01 16:09:16
4860
发布2023-05-01 16:09:16
举报
文章被收录于专栏:杂烩杂烩

一、概述

    在实际业务场景中,有时会需要统一增加一些Controller的前缀,比如统一增加V1版本号,或者按照不同业务增加不同的前缀,比如系统服务/system,用户服务/user。但是项目时间比较久,代码比较多,本着开闭原则,利用SpringMVC的WebMvcRegistrations和RequestMappingHandlerMapping类来通过增加配置类优雅实现此需求。

二、实现

配置文件配置形式如下:

代码语言:javascript
复制
#api路径前缀
api.url.perfixs.com.shy.api.system.controller=/api/system
api.url.perfixs.com.shy.api.user.controller=/api/user

配置加载类

代码语言:javascript
复制
@Configuration
@ConfigurationProperties(prefix = "api.url")
public class UrlPerfixProperties {
	private Map<String, String> perfixs = new HashMap<>();

	/**
	 * @return the perfixs
	 * @author javascc@126.com
	 * @date 2023年4月26日 上午10:26:33
	 */
	public Map<String, String> getPerfixs() {
		return this.perfixs;
	}

	/**
	 * @author javascc@126.com
	 * @date 2023年4月26日 上午10:26:33
	 * @param perfixs the perfixs to set
	 */
	public void setPerfixs(Map<String, String> perfixs) {
		this.perfixs = perfixs;
	}

	/*
	 * (非 Javadoc) <p>Title: toString</p> <p>Description: </p>
	 * 
	 * @return
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "UrlPerfixProperties [perfixs=" + this.perfixs + "]";
	}

}

核心实现

代码语言:javascript
复制
public class UrlPerfixHandlerMapping extends RequestMappingHandlerMapping {
	private UrlPerfixProperties properties;

	/**
	 * <p>
	 * Title:
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @author javascc@126.com
	 * @date 2023年4月26日 上午10:35:17
	 * @param properties
	 */
	public UrlPerfixHandlerMapping(UrlPerfixProperties properties) {
		super();
		this.properties = properties;
	}

	@Override
	protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
		RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
		if (info != null) {
			for (String packageNameConfig : this.properties.getPerfixs().keySet()) {
				if (handlerType.getPackage().getName().startsWith(packageNameConfig)) {
                    //注意以前版本可以不配置
					BuilderConfiguration configuration = new BuilderConfiguration();
					configuration.setPatternParser(PathPatternParser.defaultInstance);
					return RequestMappingInfo.paths(this.properties.getPerfixs().get(packageNameConfig))
							.options(configuration).build().combine(info);
				}
			}
		}
		return info;
	}
}

注入

代码语言:javascript
复制
@Configuration
public class UrlPerfixConfig implements WebMvcRegistrations {
	@Resource
	private UrlPerfixProperties properties;

	@Override
	public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
		return new UrlPerfixHandlerMapping(this.properties);
	}
}

三、验证

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-04-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、概述
  • 二、实现
  • 三、验证
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档