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接口可以获取当前服务器的域名。

相关推荐
程序员爱钓鱼3 小时前
Python编程实战:实现一个 Excel 批量处理工具(桌面实用脚本)
后端·python·ipython
q***23573 小时前
Spring Boot+Vue项目从零入手
vue.js·spring boot·后端
风象南3 小时前
Spring Boot + MyBatis:实现数据库字段级加密
后端
q***07143 小时前
Spring Boot管理用户数据
java·spring boot·后端
Victor3564 小时前
Redis(129)Redis的字典(Dictionary)是如何实现的?
后端
Victor3564 小时前
Redis(128)Redis的跳表(Skip List)是如何实现的?
后端
q***925111 小时前
Spring Cloud Data Flow 简介
后端·spring·spring cloud
m0_6398171512 小时前
基于springboot火锅店管理系统【带源码和文档】
java·spring boot·后端
会编程的林俊杰12 小时前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
码事漫谈12 小时前
C++循环结构探微:深入理解while与do...while
后端