FAQ
如何快速搭建一个spring boot项目?
使用Spring Initializr可以快速获取一个spring boot项目。下载解压生成的项目文件,可以看到与普通maven项目相比
使用spring boot搭建的jar项目有以下不同
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mygroup</groupId>
<artifactId>myapplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myprojectname</name>
<description>name for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mygroup</groupId>
<artifactId>myartifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>myprojectname</name>
<description>name for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-
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
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
- 增加
spring-boot-starter-log4j2
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
-
src\main\resources
目录下增加log4j2-spring.xml
作为log4j2的配置文件
如何覆盖spring-boot-dependencies
推荐的依赖版本?
在pom.xml
中的properties
标签中明确指定自己想要的版本
<properties>
<mysql.version>5.1.27</mysql.version>
</properties>
如何执行使用spring boot插件打包而成的jar包?
查看相关文档可以知道:使用spring boot的项目打包时,会生成一种特殊格式的jar文件,这使得执行jar文件变得相当简单,只需命令行执行:
java -jar target/myapplication-0.0.1-SNAPSHOT.jar
还可以通过在pom文件中修改配置:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
这样打包而成的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
网友评论