项目基于springboot 2.2.5.RELEASE版本,官网文档链接地址如下:
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle
使用maven作为项目的构建工具,创建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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.king</groupId> <artifactId>springboot-demo</artifactId> <version>1.0-SNAPSHOT</version> <name>springboot-demo</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build> </project>
添加Sample代码
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class HelloWorld { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(HelloWorld.class, args); } }
执行mvn clean compile对项目进行编译
执行mvn spring-boot:run运行程序,访问http://localhost:8080可以查看到Hello World!字样,即表示应用启动成功
执行mvn package spring-boot:repackage可在target目录下生成可执行jar包,包名为${artifactId}-${version}.jar,如果使用上述pom.xml,生产的jar名为springboot-demo-1.0-SNAPSHOT.jar
执行java -jar target/springboot-demo-1.0-SNAPSHOT.jar即可运行,访问http://localhost:8080可以查看到Hello World!