第一步:创建maven工程,引入需要的依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

第二步:创建controller

package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @author zhr_java@163.com
* @date 2022/3/29 20:50
*/
@RestController
@RequestMapping("/person")
public class PersonController {
@GetMapping("get_person")
public String getPerson(Integer numbers) {
String ip = getIp();
System.out.println("请求的ip是:"+ip);
return ip;
}
//获取请求对象
public static HttpServletRequest getRequest() {
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
return null;
} else {
return requestAttributes.getRequest();
}
}
//获取请求的ip地址
public static String getIp() {
HttpServletRequest request = getRequest();
if (request == null) {
return "127.0.0.1";
} else {
return request.getRemoteHost();
}
}
}

第三步:启动项目,并在浏览器访问该接口
在这里插入图片描述
在这里插入图片描述
第四步:做成切面,可以对每个接口的请求次数做出统计
第五步:打包上传到服务器测试

关注我的公众号SpaceObj 领取idea系列激活码

posted on   张伯灵  阅读(910)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)



点击右上角即可分享
微信分享提示