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";
    }
}
相关推荐
abcefg_h20 分钟前
MCP 实战指南:如何在项目中使用 Model Context Protocol 及其通信原理
开发语言·后端·golang·mcp
AI多Agent协作实战派24 分钟前
AI多Agent协作系统实战(四十八):改个名,整个AI团队都不认识人了
后端
wei_shuo40 分钟前
KES 故障诊断与应急响应:问题排查、根因分析与应急预案
后端
LinMINGJing00741 分钟前
postgre分区方式
后端
王中阳Go1 小时前
业务代码凭什么不能直接调 Agent?——我在律所 AI 项目里做的 Harness 运行时治理
人工智能·后端·程序员
小僧景贤1 小时前
嵌入式网络协议 | MQTT、CoAP与HTTP协议深度解析(原理、差异、工程选型、代码实操)
mqtt·http·物联网协议·coap·网络协议选型
宠友信息1 小时前
IM系统开发技术路线分析,即时通讯源码助力快速搭建聊天应用
java·spring boot·redis·websocket·mysql·uni-app·vue
SomeB1oody2 小时前
【RustyML入门】7.2. 深入模型持久化
开发语言·后端·机器学习·rust·教程
知几蜗牛2 小时前
0 后端 · 0 数据库 · 0 备案:用 AI 两天搓出的股票管理系统,开源了
前端·后端·llm
风流 少年2 小时前
Spring AI 2.0:阿里云百炼平台(工作流应用)
java·后端·spring