springboot 项目获取当前服务的IP端口地址

springboot 项目获取当前服务的IP端口地址

package com.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @description: 获取当前服务的IP端口
 * @date: 2019/7/17 14:27
 * @author: 
 */
@Component
public class ServerConfig {

    @Value("${server.port}")
    private int serverPort;

    public String getUrl() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "http://"+address.getHostAddress() +":"+this.serverPort;
    }

}

你可能感兴趣的:(springBoot)