FAQ
如何快速搭建一个spring boot项目?
使用Spring Initializr可以快速获取一个spring boot项目。下载解压生成的项目文件,可以看到与普通maven项目相比
使用spring boot搭建的jar项目有以下不同
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
com.mygroup
myapplication
0.0.1-SNAPSHOT
myprojectname
name for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
-
src\main\java
目录下带main函数的java类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
src\main\resources
目录下需要一个名称为application.properties
的配置文件src\test\java
目录下的测试类
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ApplicationTests {
@Test
void contextLoads() {
}
}
使用spring boot搭建的war项目有以下不同
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
com.mygroup
myartifact
0.0.1-SNAPSHOT
war
myprojectname
name for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
-
src\main\java
目录下带main函数的java类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
-
src\main\java
目录下继承SpringBootServletInitializer
的java类
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
src\main\resources
目录下需要一个名称为application.properties
的配置文件,同时还有两个文件夹static
和templates
src\test\java
目录下的测试类
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ApplicationTests {
@Test
void contextLoads() {
}
}
如何使用log4j2作为默认日志工具?
-
spring-boot-starter
依赖下排除spring-boot-starter-logging
依赖
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
- 增加
spring-boot-starter-log4j2
依赖
org.springframework.boot
spring-boot-starter-log4j2
-
src\main\resources
目录下增加log4j2-spring.xml
作为log4j2的配置文件
如何覆盖spring-boot-dependencies
推荐的依赖版本?
在pom.xml
中的properties
标签中明确指定自己想要的版本
5.1.27
如何执行使用spring boot插件打包而成的jar包?
查看相关文档可以知道:使用spring boot的项目打包时,会生成一种特殊格式的jar文件,这使得执行jar文件变得相当简单,只需命令行执行:
java -jar target/myapplication-0.0.1-SNAPSHOT.jar
还可以通过在pom文件中修改配置:
org.springframework.boot
spring-boot-maven-plugin
true
这样打包而成的jar文件就成为了一个“完全可执行的jar包”,可以直接在命令行这么执行:
./target/myapplication-0.0.1-SNAPSHOT.jar
读取配置文件时中文乱码?
放弃.properties
格式,使用.yaml
格式作为配置文件的文件格式。.properties
格式默认会使用ISO 8859-1
作为字符编码,而.yaml
格式默认支持UTF-8
编码。
接口返回的JSON数据中日期不是时间戳?
配置文件中加入以下配置:
spring.jackson.serialization.write-dates-as-timestamps=true
参考资料
- Spring Boot Reference Documentation
- nohup: redirecting stderr to stdout的解决办法
- Spring Boot default properties encoding change?