Springboot项目使用Undertow替换内置Tomcat服务器,实现RESTFUL接口web应用

Maven实例:pom.xml文件中添加更换依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--整合web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

application.yml文件配置相关参数

server:
  port: 15026
  undertow:
    accesslog:
      enabled: false
    direct-buffers: true # 是否分配的直接内存(NIO直接分配的堆外内存)
    buffer-size: 1024  #每块buffer的空间大小,越小的空间被利用越充分
    threads:
      worker: 20 # 阻塞任务线程池, 它的值设置取决于系统线程执行任务的阻塞系数,默认值是IO线程数*8
      io: 4  # CPU有几核,就填写几。
  servlet:
    context-path: /undertow

构建Springboot项目启动器

@SpringBootApplication
public class AppRunning {

    public static void main(String[] args) {
        SpringApplication.run(AppRunning.class, args);
    }

}

建立RESTFUl的Controller

/**
 * @description: 短连接HTTP层 RestFul API
 * @author: GuoTong
 * @createTime: 2023-05-16 21:31
 * @since JDK 1.8 OR 11
 **/
@RestController
@RequestMapping("restful")
public class BootStrapController {

    @GetMapping("api")
    public HashMap<String, String> hello() {
        HashMap<String, String> map = new HashMap<>();
        map.put("data", "很高兴认识你");
        map.put("code", "200");
        map.put("author", "郭童");
        return map;
    }
}

测试 http://localhost:15026/undertow/restful/api

image

posted on   白嫖老郭  阅读(111)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示