spring boot 获取服务器域名

在Spring Boot中获取服务器域名可以使用HttpServletRequest对象来获取。可以通过注入HttpServletRequest对象,然后调用getRequestURL()方法来获取当前请求的完整URL,然后再通过处理URL字符串来提取出域名。

示例代码如下:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
public class DomainController {

    @Autowired
    private HttpServletRequest request;

    @GetMapping("/domain")
    public String getDomain() {
        StringBuffer url = request.getRequestURL();
        String domain = url.substring(0, url.indexOf("/", 8));
        return domain;
    }
}

通过访问/domain接口可以获取当前服务器的域名。

相关推荐
陈随易1 天前
Postman 被曝自动往 Claude Code 和 Codex 装插件
前端·后端·程序员
云-缩影1 天前
拆解 AI 图像生成应用的后端架构:ECS、OSS 和大模型 API 怎么配合
java·spring boot·后端
考虑考虑1 天前
Posrgresql恢复.backup文件
数据库·后端·postgresql
无奈何杨1 天前
风控决策系统决策流,策略模式,权重、顺序、投票、最坏
后端
AI开源软件1 天前
SpringBoot3+Vue3 财务管理系统如何设计:从业务单据到总账的模块划分
spring boot·vue3·领域建模·会计凭证·ruoyi office·业财协同·财务系统设计
Patrick_Wilson1 天前
Go 为什么需要 defer?从函数怎么收尾说起
后端·go·编程语言
苏三说技术1 天前
DeepSeek Harness入驻B站,评论区被干成了大型插件集市!
后端
橙序员小站1 天前
我一句话让 AI 把公众号工作台搭完了:部署实测记录
后端
Patrick_Wilson1 天前
从文本输出理解 Go 方法与接口
后端·go·编程语言
QCoding1 天前
企业级Agent Evaluation实战:Agent能跑就能上线?AI Agent到底怎么测?
后端·spring