SpringBoot定制修改Servlet容器

时间:2023-03-09 01:03:39
SpringBoot定制修改Servlet容器

1.如何修改Servlet容器的相关配置:

第一种:在application.properties中修改和server有关的配置(ServerProperties提供);

server.port=8081
server.context‐path=/crud
server.tomcat.uri‐encoding=UTF‐8
//通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx

第二种:编写一个EmbeddedServletContainerCustomizer(嵌入式Servlet容器定制器);

@Bean

public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){

  return new EmbeddedServletContainerCustomizer() {

    //定制嵌入式的Servlet容器相关的规则

    @Override

    public void customize(ConfigurableEmbeddedServletContainer container) {//传入一个可配置的嵌入式容器

      container.setPort(8083);

    }

  };

}

2.如何更改默认servlet容器

该默认Tomcat为Jetty,也可以更改为Undertow

<!‐‐ 引入web模块 ‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions>
<exclusion>
<!‐‐去除其Tomcat容器‐‐>
<artifactId>spring‐boot‐starter‐tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!‐‐引入其他的Servlet容器‐‐>
<dependency>
<artifactId>spring‐boot‐starter‐jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>