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";
    }
}
相关推荐
卷无止境22 分钟前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
程序员威哥22 分钟前
零基础玩转西门子PLC:C#手撕S7协议,打造工业数据采集神器
后端
用户7428372563322 分钟前
【Ambari Plus】Step9—AmbariServer 初始化
后端
wuxinzhe76cmd30 分钟前
JVM 垃圾回收基础:从 STW 到分代收集(附 G1/ZGC 导读)
后端
MrSYJ35 分钟前
TCP协议理解
后端·tcp/ip
boolean的主人36 分钟前
超实用!5 个 MySQL 索引优化实战场景(附 10 万测试数据)
后端
BBmmo37 分钟前
JDBC基础篇
后端
用户642780069378839 分钟前
elpis-core 第一阶段学习心得与收获
后端
kfaino41 分钟前
码农的AI翻身·前传 一个大模型从出生到上岗的全过程
后端·aigc
IT_陈寒1 小时前
Vue的这个响应式陷阱让我熬到凌晨三点
前端·人工智能·后端