iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >在SpringBoot项目中实现给所有请求加固定前缀
  • 491
分享到

在SpringBoot项目中实现给所有请求加固定前缀

2024-04-02 19:04:59 491人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录给所有请求加个固定前缀知识小锦囊配置文件读取(固定前缀)1.配置文件所有固定前缀的都可以使用2.SpringBoot实体类3.使用方式给所有请求加个固定前缀 在开发中,可能会遇到

给所有请求加个固定前缀

开发中,可能会遇到需要配置项目前缀的问题,虽然我们可以在Controller控制器方法中给所有请求加前缀,但是不仅比较麻烦,而且在某种环境下是没什么用处,形同虚设

接下来,教你在配置文章中只需短短一小行代码配置即可生效

知识小锦囊

在yml配置文件中加入配置:

server.servlet.context-path: /需要设置的路径前缀

再重启测试即可生效

配置文件读取(固定前缀)

1.配置文件所有固定前缀的都可以使用

SpringBoot自动注入实体类如下配置

配置文件:application.properties

固定前缀: sys.test.config

配置信息:

sys.test.config.industryKey=aaa
sys.test.config.systemName=bbb
sys.test.config.downloadUrl=ccc
sys.test.config.traceDomain=DDD

2.springBoot实体类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "sys.test.config")
public final class ResourceContainer { 
    private String industryKey;    
    private String systemName;    
    private String downloadUrl;    
    private String traceDomain;    
    public String getIndustryKey() {
        return industryKey;
    }
 
    public void setIndustryKey(String industryKey) {
        this.industryKey = industryKey;
    }
 
    public String getSystemName() {
        return systemName;
    }
 
    public void setSystemName(String systemName) {
        this.systemName = systemName;
    }
 
    public String getDownloadUrl() {
        return downloadUrl;
    }
 
    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }
 
    public String getTraceDomain() {
        return traceDomain;
    }
 
    public void setTraceDomain(String traceDomain) {
        this.traceDomain = traceDomain;
    }
}

3.使用方式

@Autowired
private ResourceContainer resourceContainer;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。 

--结束END--

本文标题: 在SpringBoot项目中实现给所有请求加固定前缀

本文链接: https://www.lsjlt.com/news/139617.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 在SpringBoot项目中实现给所有请求加固定前缀
    目录给所有请求加个固定前缀知识小锦囊配置文件读取(固定前缀)1.配置文件所有固定前缀的都可以使用2.SpringBoot实体类3.使用方式给所有请求加个固定前缀 在开发中,可能会遇到...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作