Springboot api http并发测试请求

pom.xml

复制代码
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

线程发起请求

复制代码
package com.example.demo;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Callable;

public class ConcurrentRequestTask implements Callable<String> {

    private final String url;

    public ConcurrentRequestTask(String url) {
        this.url = url;
    }

    @Override
    public String call() throws Exception {
        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求Header
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer token123");
        headers.set("Content-Type","application/json");

        // 设置请求参数
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("key1", "value1");
        map.add("key2", "value2");

        // 创建请求实体
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        String result = responseEntity.getBody();
        return result;
    }
}

Test

复制代码
package com.example.demo;

import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ConcurrentRequestTest {

    public static void main(String[] args) throws Exception {
        String url = "http://localhost:8080/api";

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        int taskCount = 10;

        Future<String>[] futures = new Future[taskCount];
        for (int i = 0; i < taskCount; i++) {
            futures[i] = executorService.submit(new ConcurrentRequestTask(url));
        }

        for (Future<String> future : futures) {
            System.out.println(future.get());
        }

        executorService.shutdown();
    }
}

API

复制代码
package com.example.demo.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
public class ApiController {

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/api")
    public String apiEndpoint(@RequestBody Map<String,Object> map) {
        System.out.println("param:"+map.toString());
        // 这里可以添加业务逻辑
        return "Response";
    }
}
相关推荐
白衣鸽子20 分钟前
【基础数据篇】数据遍历大师:Iterator模式
后端·设计模式
用户40993225021223 分钟前
想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
后端·ai编程·trae
艾菜籽29 分钟前
SpringMVC练习:加法计算器与登录
java·spring boot·spring·mvc
xuejianxinokok30 分钟前
什么是代数类型 ? java为什么要添加record,Sealed class 和增强switch ?
后端·rust
洛小豆30 分钟前
Git打标签仓库看不到?她说:豆子,你又忘了加 --tags!
git·后端·github
LawsonJin1 小时前
springboot实现微信小程序支付(服务商和普通商户模式)
spring boot·后端·微信小程序
福大大架构师每日一题1 小时前
2025-10-16:有向无环图中合法拓扑排序的最大利润。用go语言,给定一个由 n 个节点(编号 0 到 n-1)构成的有向无环图,边集合用二维数组 edge
后端
只玩代码1 小时前
技术拆解:基于 Rokid CXR-M SDK 构建“AI 实时翻译眼镜伴侣”核心逻辑
后端
码码宇1 小时前
技术拆解:Rokid CXR-M SDK 如何构建流畅AR演讲提词功能
后端
沐眼1 小时前
技术拆解:Rokid CXR-M SDK 构建 AI 智能提词眼镜助手连接到场景落地
后端