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
作者:隔壁老郭
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
Java入门到入坟
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!