赞
踩
在Spring Boot中获取服务器域名可以使用HttpServletRequest对象来获取。可以通过注入HttpServletRequest对象,然后调用getRequestURL()方法来获取当前请求的完整URL,然后再通过处理URL字符串来提取出域名。
示例代码如下:
- 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
接口可以获取当前服务器的域名。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。