Commit 2f535082 by RuoYi

获取完整的服务地址

parent d872c66d
package com.ruoyi.framework.config; package com.ruoyi.framework.config;
import org.springframework.boot.web.context.WebServerInitializedEvent; import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.net.InetAddress; import com.ruoyi.framework.util.ServletUtils;
import java.net.UnknownHostException;
/** /**
* 服务相关配置 * 服务相关配置
...@@ -13,28 +11,23 @@ import java.net.UnknownHostException; ...@@ -13,28 +11,23 @@ import java.net.UnknownHostException;
* *
*/ */
@Component @Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> public class ServerConfig
{ {
private int serverPort; /**
* 获取完整的请求路径,包括:域名,端口,上下文访问路径
*
* @return 服务地址
*/
public String getUrl() public String getUrl()
{ {
InetAddress address = null; HttpServletRequest request = ServletUtils.getRequest();
try return getDomain(request);
{
address = InetAddress.getLocalHost();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
return "http://" + address.getHostAddress() + ":" + this.serverPort;
} }
@Override public static String getDomain(HttpServletRequest request)
public void onApplicationEvent(WebServerInitializedEvent event)
{ {
this.serverPort = event.getWebServer().getPort(); StringBuffer url = request.getRequestURL();
String contextPath = request.getServletContext().getContextPath();
return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment