首页 > 作文

SpringBoot2.x实现给Controller的RequestMapping添加统一前缀

更新时间:2023-04-05 01:51:09 阅读: 评论:0

目录
给controller的requestmapping添加统一前缀总结一下 有几个方法springboot项目添加全局前缀spring的配置spring boot的配置

给controller的requestmapping添加统一前缀

如何给controller的requestmapping添加统一前缀,比如”/api”,为什么要添加统一访问前缀,其实是为了后面的接口的管理。

切记:约定与规范好过一切技术处理 !

比如:

项目a必须所有访问接口url必须增加 /api/projecta/项目b必须所有访问接口url必须增加 /api/projectb/看到url里面含有/api 表示访问后端接口服务,/projecta/ 一看就知道是项目a提供的服务接口。

总结一下 有几个方法

1、在配置application.yml文件中添加:

 rvlet:  context-path: /api #(不同springboot版本会有区别,这里是采用2.x)

但是这个其实是整个项目访问前缀,如果你有静态资源也需要增加 /api 这个前缀访问。

2、通过nginx 和 你的网关层 添加统一的访问路径前缀,这个不多说了。

3、springmvc 可以实现 webmvcconfigurer 接口中的 configurepathmatch 方法来实现添加统一路径前缀。

package com.middol.webba.framework.config;import com.middol.webba.framework.annotation.apirestcontroller;import com.middol.webba.framework.annotation.reportrestcontroller;import com.middol.webba.framework.properties.apipathproperties;import org.springframework.context.annotatio孝心名言n.configuration;import org.springframework.web.rvlet.config.annotation.pathmatchconfigurer;import org.springframework.web.rvlet.config.annotation.webmvcconfigurer;import javax.annotation.resource;/** * 配置统一的后台接口访问路径的前缀 * @author c西 */@configurationpublic class mywebmvcconfig implements webmvcconfigurer {    @resource    private apipathproperties apipathproperties;    @override    public void configurepathmatch(pathmatchconfigurer configurer) {        configurer                .addpathprefix(apipathproperties.getglobalprefix(),c -> c.isannotationprent(apirestcontroller.class))                .addpathprefix(apipathproperties.getreportprefix(),c -> c.isannotationprent(reportrestcontroller.class));    }}

意思是 对有 @apirestcontroller 注解的 controller 添加 /api前缀,对有@reportrestcontroller 注解的controller添加 /api/report 前缀。

@apirestcontroller 和 @reportrestcontroller 是自定义注解继承 @restcontroller注解。

package com.middol.webba.framework.annotation;import org.springframework.core.annotation.ali楚乔传分集剧情asfor;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller;import java.lang.annotation.*;/** * controller层统一使用该注解 * @author c西 */@target(elementtype.type)@retention(retentionpolicy.runtime)@documented@restcontroller@requestmappingpublic @interface apirestcontroller {    /**     * alias for {@link requestmapping#name}.     */    @aliasfor(annotation = requestmapping.class)    饮酒 其五string name() default "";    /**     * alias for {@link requestmapping#value}.     */    @aliasfor(annotation = requestmapping.class)    string[] value() default {};    /**     * alias for {@link requestmapping#path}.     */    @aliasfor(annotation = requestmapping.class)    string[] path() default {};}

然后 你的业务controller 层代码添加 @apirestcontroller 即可,如下:

@api(value = "demour增删改查接口", tags = "【测试接口】")@apirestcontroller("demour")public class demourcontroller extends bacontroller{}

其中 apipathproperties 是统一前缀名称管理,可以在yml中修改,我这里设置了两个 一般的crud接口 /api , 报表服务接口 统一为 /api/report,各自看各自服务定到底设置几个。

package com.middol.webba.framework.properties;import lombok.data;import org.springframework.boot.context.properties.configurationproperties;import org.springframework.stereotype.component;/** * 接口路径前缀配置 * @author c西 */@component@configurationproperties(prefix = "api.path")@datapublic class apipathproperties {    string globalprefix = "api";    string reportprefix = "api/report";}

application.yml文件中添加如下

##屌丝 专门针对 controller层接口路径前缀全局配置api: path:  global-prefix: api  report-prefix: api/report

springboot项目添加全局前缀

spring的配置

spring.application.name: article (spring boot下无效)

spring boot的配置

(springboot你自己设置的前缀名称)

properties文件

rver.rvlet.context-path: /springboot

yml文件

rver: rvlet:  context-path: /sprin社区志愿服务活动内容gboot

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-05 01:51:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/347d433d88e5732ad2837437f445ddbb.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:SpringBoot2.x实现给Controller的RequestMapping添加统一前缀.doc

本文 PDF 下载地址:SpringBoot2.x实现给Controller的RequestMapping添加统一前缀.pdf

标签:前缀   接口   注解   路径
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图