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";
    }
}
相关推荐
明月_清风2 分钟前
🔐 Solidity 完全指南:关键语法解析与智能合约中的核心作用
后端·web3
geovindu19 分钟前
CSharp: Iterative Algorithms
开发语言·后端·算法·c#·.net·迭代算法
雪隐42 分钟前
AI股票小助手10-我用代码管住自己的手:一个普通人的市场观察笔记
前端·人工智能·后端
艾斯特_1 小时前
混合检索与重排:提升召回与排序质量
后端·python·ai
TlSfoward1 小时前
如何用 TLS 与 HTTP 指标降低误伤 TLSFOWARD抓包工具
网络·网络协议·http
Ai拆代码的曹操2 小时前
RocketMQ 消息堆积排查实战:从 20 个消费者线程卡死到 rebalance 死亡螺旋
后端·rocketmq
吃饱了得干活2 小时前
@Transactional 又失效了?把这 8 个坑全填了!
java·后端·spring
颜进强2 小时前
从零搭建私人 RAG 知识库:让项目决策真正“可检索、可追溯”
前端·后端·ai编程
颜进强2 小时前
Embedding 模型介绍:热门模型对比与应用场景
前端·后端·ai编程
颜进强2 小时前
LanceDB 基础使用:用 TypeScript 完成第一次向量检索
前端·后端·ai编程