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 2023-05-18 20:05  白嫖老郭  阅读(79)  评论(0编辑  收藏  举报

导航