我是靠谱客的博主 温柔香烟,这篇文章主要介绍springboot 项目获取当前服务的IP端口地址,现在分享给大家,希望可以做个参考。

package com.summergao.config;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 获取当前服务的IP端口
 *
 * @author SummerGao
 */
@Configuration
public class ServerConfig {
    @Value("${tomcat.port}")
    private Integer port;

    @Value("${tomcat.contextPath}")
    private String contextPath;

    /**
     * 项目获取当前服务的IP端口地址
     *
     * @return
     */
    public  String getUrl() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "http://" + address.getHostAddress() + ":" + port + "/" + contextPath;
    }
}

最后

以上就是温柔香烟最近收集整理的关于springboot 项目获取当前服务的IP端口地址的全部内容,更多相关springboot内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(135)

评论列表共有 0 条评论

立即
投稿
返回
顶部