23.启动内嵌Jetty服务器

 

SpringBoot默认使用Tomcat服务器,但是也可以使用其他内嵌的服务器。

启动内嵌的Jetty服务器:

1.修改pom.xml,排除tomcat的依赖,引入jetty的启动器

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter-web包含spring-boot-starter-tomcat

修改pom.xml,排除tomcat的依赖,同时引入jetty的启动器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<project>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

<!-- 排除Tomcat的依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- 引入Jetty的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
</project>

2.yml配置

1
2
3
4
5
6
7
8
server:
port: 8081 # 端口的配置
# tomcat:
# uri-encoding: UTF-8 # 编码的配置
servlet:
context-path: /test/demo # 相当于配置了项目部署的名字
session:
timeout: 1800 # HttpSession的超时时间

运行启动类,启动项目

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
32

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)

2022-05-31 15:13:41.063 INFO 6980 --- [ restartedMain] com.example.demo.Application : Starting Application using Java 1.8.0_271 on DESKTOP-IOB28AF with PID 6980 (E:\SpringToolSuite\project\demo-4.8.1\09_springboot_web_conf_my\target\classes started by Tom in E:\SpringToolSuite\project\demo-4.8.1\09_springboot_web_conf_my)
2022-05-31 15:13:41.065 INFO 6980 --- [ restartedMain] com.example.demo.Application : No active profile set, falling back to default profiles: default
2022-05-31 15:13:41.108 INFO 6980 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-05-31 15:13:41.108 INFO 6980 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-05-31 15:13:41.704 INFO 6980 --- [ restartedMain] org.eclipse.jetty.util.log : Logging initialized @1388ms to org.eclipse.jetty.util.log.Slf4jLog
2022-05-31 15:13:41.830 INFO 6980 --- [ restartedMain] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8081
2022-05-31 15:13:41.833 INFO 6980 --- [ restartedMain] org.eclipse.jetty.server.Server : jetty-9.4.39.v20210325; built: 2021-03-25T14:42:11.471Z; git: 9fc7ca5a922f2a37b84ec9dbc26a5168cee7e667; jvm 1.8.0_271-b09
2022-05-31 15:13:41.857 INFO 6980 --- [ restartedMain] o.e.j.s.h.ContextHandler.application : Initializing Spring embedded WebApplicationContext
2022-05-31 15:13:41.857 INFO 6980 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 748 ms
2022-05-31 15:13:41.921 INFO 6980 --- [ restartedMain] org.eclipse.jetty.server.session : DefaultSessionIdManager workerName=node0
2022-05-31 15:13:41.921 INFO 6980 --- [ restartedMain] org.eclipse.jetty.server.session : No SessionScavenger set, using defaults
2022-05-31 15:13:41.922 INFO 6980 --- [ restartedMain] org.eclipse.jetty.server.session : node0 Scavenging every 600000ms
2022-05-31 15:13:41.931 INFO 6980 --- [ restartedMain] o.e.jetty.server.handler.ContextHandler : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@1548eee4{application,/test/demo,[org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory$LoaderHidingResource@6670b655],AVAILABLE}
2022-05-31 15:13:41.931 INFO 6980 --- [ restartedMain] org.eclipse.jetty.server.Server : Started @1617ms
2022-05-31 15:13:42.101 INFO 6980 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-05-31 15:13:42.169 INFO 6980 --- [ restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2022-05-31 15:13:42.263 INFO 6980 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-05-31 15:13:42.278 INFO 6980 --- [ restartedMain] o.e.j.s.h.ContextHandler.application : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-05-31 15:13:42.278 INFO 6980 --- [ restartedMain] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-05-31 15:13:42.279 INFO 6980 --- [ restartedMain] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2022-05-31 15:13:42.478 INFO 6980 --- [ restartedMain] o.e.jetty.server.AbstractConnector : Started ServerConnector@65347586{HTTP/1.1, (http/1.1)}{0.0.0.0:8081}
2022-05-31 15:13:42.479 INFO 6980 --- [ restartedMain] o.s.b.web.embedded.jetty.JettyWebServer : Jetty started on port(s) 8081 (http/1.1) with context path '/test/demo'
2022-05-31 15:13:42.489 INFO 6980 --- [ restartedMain] com.example.demo.Application : Started Application in 1.768 seconds (JVM running for 2.175)

controller

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
32
33
34
35
package com.example.demo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.vo.Student;

@Controller
@RequestMapping("index")
public class IndexController {

@RequestMapping(value = "getStudentInfo", method = RequestMethod.GET)
@ResponseBody
public Student getStudentInfo() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date birthday = format.parse("2020-01-01 10:10:10");

Student student = new Student();
student.setId(1);
student.setName("John");
student.setBirthday(birthday);

return student;
}

}

浏览器访问http://localhost:8081/test/demo/index/getStudentInfo