1 创建pom.xml
parent:org.springframework.boot 包含启动的依赖
添加依赖,如 spring-boot-starter-web
mvn dependency:tree 查看依赖
mvn spring-boot:run 启动服务
build添加:spring-boot-maven-plugin依赖
mvn package:打包,生成可运行jar
命令启动 java -jar target/myproject-0.0.1-SNAPSHOT.jar
main方法为启动入口
@RestController表示接受请求,并返回一个字符串,字符串就是response
@EnableAutoConfiguration表示自动推测
@RequestMapping作用在方法上,指定请求的路由
<?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.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <!-- Additional lines to be added here... --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World! "; } public static void main(String[] args) { SpringApplication.run(Example.class, args); } }