SpringBoot中如何使用Jetty呢?
下文笔者讲述SpringBoot中引入Jetty的方法及示例分享
1.排除tomcat依赖,引入Jetty依赖 2.配置文件设置相关配置 3.编写相关的配置代码例:Spring Boot引入Jetty的示例
添加spring-boot-starter-jetty依赖 >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-jetty>/artifactId< >/dependency< 使用Gradle构建 可以使用以下方式达到同样的效果: configurations { compile.exclude module: "spring-boot-starter-tomcat" } dependencies { compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT") compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT") } 配置Jetty参数# 可在application.properties配置文件 里配置相关参数,去覆盖Jetty默认使用的运行参数 application.properties server.port=8080 server.servlet.context-path=/home ####Jetty specific properties######## server.jetty.acceptors= # Number of acceptor threads to use. server.jetty.max-http-post-size=0 # Maximum size in bytes of the HTTP post or put content. server.jetty.selectors= # Number of selector threads to use. 可使用JettyEmbeddedServletContainerFactory bean以编程的方式去配置这些参数 @Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory(); jettyContainer.setPort(9000); jettyContainer.setContextPath("/home"); return jettyContainer; } Spring boot 2.0.0.RELEASE版本之后的更新# 以上代码针对的是Spring Boot Snapshot版本 在Spring boot 2.0.0.RELEASE版本之后 应该使用 ConfigurableServletWebServerFactory 和 JettyServletWebServerFactory类去配置Jetty参数: 创建ConfigurableServletWebServerFactory Bean @Bean public ConfigurableServletWebServerFactory webServerFactory() { JettyServletWebServerFactory factory = new JettyServletWebServerFactory(); factory.setPort(9000); factory.setContextPath("/myapp"); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); return factory; }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。