Springboot获取端口号以及相关参数(配置类+工具类)+前后端发送请求
IP,端口,URL在Java中:here
1.配置文件方式,在yml配置文件中配置好后,在config类中使用,注意配置类的参数方法除了set都要使用static修饰。
@Component+
@ConfigurationProperties(prefix = "")+字段名与配置文件参数名相同注解
然后在需要的地方创建config实体类对象通过属性或方法的方式调用就行。
tips:
1.如果prefix不同,可以在配置文件中相互调用。
配置文件中:(project前缀引用server.port)
project:
file: E:/SpringbootMybatis/mg/src/main/resources/static
port: ${server.port}
server:
port: 10100 # 配置启动端口号
config配置类中:
@Component
@ConfigurationProperties(prefix = "project")
public class MyConfig {
private static String file;
private static String port;
public static String getFile() {
return file;
}
public void setFile(String file) {
MyConfig.file = file;
}
public static String getPort() {
return port;
}
public void setPort(String port) {
MyConfig.port = port;
}
}
2.配置类中不支持使用Lombok的@Data注解
比如:

project:
# 这里配置文件资源路径
file: E:/SpringbootMybatis/mg/src/main/resources/static

@Component @ConfigurationProperties(prefix = "project") public class MyConfig { private static String file; public static String getFile() { return file; } public void setFile(String file) { MyConfig.file = file; } }
2.引用工具类

@Configuration public class useage { /** * ------------ * 查询ip地址 * ------------ */ public static String getIpAddr() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String ipAddress = null; try { ipAddress = request.getHeader("x-forwarded-for"); if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); if (ipAddress.equals("127.0.0.1")||ipAddress.equals("0:0:0:0:0:0:0:1")) { ipAddress = "127.0.0.1"; } } if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length() // = 15 if (ipAddress.indexOf(",") > 0) { ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); } } } catch (Exception e) { ipAddress=""; } return ipAddress; } }
然后在需要的地方调用get方法
其他工具类:
Springboot启动后输出运行端口:
1.@SpringBootApplication主体类中实现接口ApplicationListener<WebServerInitializedEvent> 并启用注解@Slf4j
2.实现方法:

@SneakyThrows//lombok封装抛异常的注解 @Override public void onApplicationEvent(WebServerInitializedEvent event) { WebServer server = event.getWebServer(); WebServerApplicationContext context = event.getApplicationContext(); Environment env = context.getEnvironment(); String ip = InetAddress.getLocalHost().getHostAddress(); int port = server.getPort(); String contextPath = env.getProperty("server.servlet.context-path"); if (contextPath == null) { contextPath = ""; } log.info("\n---------------------------------------------------------\n" + "\tApplication is running! Access address:\n" + "\tLocal:\t\thttp://localhost:{}" + "\n\tExternal:\thttp://{} : {} {}" + "\n---------------------------------------------------------\n", port, ip, port, contextPath); }
Controller层中各种url:

String IP = InetAddress.getLocalHost().getHostAddress(); System.out.println("IP = " + IP); String ipAddr1 = useage.getIpAddr(); System.out.println("ipAddr1 = " + ipAddr1);//前面引用的工具类 // 接口中添加参数HttpServletRequest request // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String uri = request.getRequestURI();//返回请求行中的资源名称 String url = request.getRequestURL().toString();//获得客户端发送请求的完整url String ip = request.getRemoteAddr();//返回发出请求的IP地址 String params = request.getQueryString();//返回请求行中的参数部分 String host = request.getRemoteHost();//返回发出请求的客户机的主机名 int port = request.getRemotePort();//返回发出请求的客户机的端口号。 System.out.println("返回请求行中的资源名称" + uri); System.out.println("获得客户端发送请求的完整url" + url); System.out.println("返回发出请求的IP地址" + ip); System.out.println("返回请求行中的参数部分" + params); System.out.println("返回发出请求的客户机的主机名" + host); System.out.println("返回发出请求的客户机的端口号" + port);
--------------------------------------springboot发送请求-----------------------------------
springboot发送请求参考:here

<!--使用RestTemplate进行发送请求,添加相关依赖 发送请求的依赖--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency>

import org.springframework.http.*; import org.springframework.http.client.ClientHttpResponse; import org.springframework.util.MultiValueMap; import org.springframework.web.client.ResponseErrorHandler; import org.springframework.web.client.RestTemplate; /** * @author:WangZiBin * @description:http发送工具类 */ public class HttpClient { /** * 向目的URL发送post请求 * @param url 目的url * @param params 发送的参数 * @return AdToutiaoJsonTokenData */ public static String sendPostRequest(String url, MultiValueMap<String, String> params){ RestTemplate client = new RestTemplate(); //新建Http头,add方法可以添加参数 HttpHeaders headers = new HttpHeaders(); //设置请求发送方式 HttpMethod method = HttpMethod.POST; // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers); //执行HTTP请求,将返回的结构使用String 类格式化(可设置为对应返回值格式的类) ResponseEntity<String> response = client.exchange(url, method, requestEntity,String .class); return response.getBody(); } /** * 向目的URL发送get请求 * @param url 目的url * @param params 发送的参数 * @param headers 发送的http头,可在外部设置好参数后传入 * @return String */ public static String sendGetRequest(String url, MultiValueMap<String, String> params,HttpHeaders headers){ RestTemplate client = new RestTemplate(); HttpMethod method = HttpMethod.GET; // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers); //执行HTTP请求,将返回的结构使用String 类格式化 ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class); return response.getBody(); } }
---------------------------------------axios同步请求-----------------------------------------
ajax发送请求可以设置请求的同步异步性,但是axios已经默认了异步请求。
这时候需要套用 async+(await 请求) 来使用,保证了async的{}主体部分是顺序同步执行
(额外记录一下,SetTimeOut计时器是循环体结束后才开始下一次的计时)
(前后端参数互通)请求参数类型详解:here
【Over】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
2020-07-31 Easyx基础:【绘图坐标+坐标转换+光标移动】(整理)+全屏鼠标投影(原创)