文章目录
端口号
默认值为8080。
server.port=8083
对于基于YAML的配置:
server:
port: 8083
我们还可以通过编程方式自定义服务器端口:
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory container) {
container.setPort(8083);
}
}
上下文路径
默认情况下,上下文路径为“/”。
server.servlet.contextPath=/springbootapp
对于基于YAML的配置:
server:
servlet:
contextPath:/springbootapp
最后–更改也可以通过编程方式完成:
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory container) {
container.setContextPath("/springbootapp");
}
}
错误页面
如果您在配置中未指定任何自定义实现,则Spring Boot会自动注册BeanBasicErrorController。
但是,当然可以配置此默认控制器:
public class MyCustomErrorController implements ErrorController {
private static final String PATH = "/error";
@GetMapping(value=PATH)
public String error() {
return "Error haven";
}
@Override
public String getErrorPath() {
return PATH;
}
}
自定义错误消息
Boot默认情况下提供*/error*映射,以合理的方式处理错误。
如果要配置更特定的错误页面,则对统一的Java DSL进行自定义错误处理提供了很好的支持:
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory container) {
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
container.addErrorPages(new ErrorPage("/errorHaven"));
}
}
在这里,我们专门处理了错误请求以匹配*/400*路径,所有其他请求都匹配公共路径。
还有一个非常简单的*/errorHaven*实现:
@GetMapping("/errorHaven")
String errorHeaven() {
return "You have reached the haven of errors!!!";
}
输出:
You have reached the haven of errors!!!
以编程方式关闭应用程序
您可以在SpringApplication的帮助下以编程方式关闭Boot应用程序*。它有一个带有两个参数的静态exit()方法:ApplicationContext和*ExitCodeGenerator:
@Autowired
public void shutDown(ExecutorServiceExitCodeGenerator exitCodeGenerator) {
SpringApplication.exit(applicationContext, exitCodeGenerator);
}
通过此实用程序方法,我们可以关闭该应用程序。
配置日志记录级别
您可以轻松地在Boot应用程序中调整日志记录级别;从版本1.2.0开始,您可以在主要属性文件中配置日志级别:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
就像标准的Spring应用程序一样,您可以通过在类路径中添加其自定义的XML或属性文件并在pom中定义库来激活不同的日志记录系统,例如Logback,log4j,log4j2等。
注册一个新的Servlet
如果要在嵌入式服务器的帮助下部署应用程序,则可以通过在常规配置中将新的Servlet公开为bean来在Boot应用程序中注册新的Servlet :
@Bean
public HelloWorldServlet helloWorld() {
return new HelloWorldServlet();
}
另外,您可以使用ServletRegistrationBean :
@Bean
public SpringHelloServletRegistrationBean servletRegistrationBean() {
SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(
new SpringHelloWorldServlet(), "/springHelloWorld/*");
bean.setLoadOnStartup(1);
bean.addInitParameter("message", "SpringHelloWorldServlet special message");
return bean;
}
在启动应用程序中配置Jetty或Undertow
Spring Boot启动程序通常使用Tomcat作为默认的嵌入式服务器。如果需要更改–您可以排除Tomcat依赖性,而包括Jetty或Undertow:
配置码头
<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>
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
JettyEmbeddedServletContainerFactory jettyContainer =
new JettyEmbeddedServletContainerFactory();
jettyContainer.setPort(9000);
jettyContainer.setContextPath("/springbootapp");
return jettyContainer;
}
配置Undertow
<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>
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
UndertowEmbeddedServletContainerFactory factory =
new UndertowEmbeddedServletContainerFactory();
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(io.undertow.Undertow.Builder builder) {
builder.addHttpListener(8080, "0.0.0.0");
}
});
return factory;
}
🍎QQ群【837324215】
🍎关注我的公众号【Java大厂面试官】,一起学习呗🍎🍎🍎
🍎个人vx【lakernote】