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";
    }
}
相关推荐
IT_陈寒4 分钟前
Java线程池用错参数,我的服务居然悄悄崩溃了
前端·人工智能·后端
光影少年16 分钟前
Express 中间件原理
后端·node.js·express
碳基修炼35 分钟前
排查记:本地 devServer 是 http,浏览器却把 302 重定向升级成了 https
前端·http
aramae1 小时前
MySQL内置函数(7)
开发语言·笔记·后端·mysql·其他
徐小夕2 小时前
存量.doc 文档怎么办?JitWord 开源转换库打通旧文档到在线协同全链路
后端·架构·github
ee又momo2 小时前
一次 JWT Token 过期续期的踩坑记录
后端
二月龙2 小时前
App 瘦身实战:图片、so 库、资源压缩,安装包体积减小 40%
后端
大勇前进2 小时前
Kotlin 协程实战避坑:90% 的人都会踩的生命周期泄漏问题
后端
苏三说技术3 小时前
Spring AI、LangChain4j、AgentScope、Embabel,哪个AI框架更好?
后端
烽学长3 小时前
(附源码)基于Springboot+vue的图书阅读分享系统的设计与实现
java·spring boot·后端