Maven创建
使用Spring Initializr创建项目的过程把很多细节都隐藏了,所以我们很难注意到Spring Boot 的本质是什么?下面仅使用maven从无到有构建一个Spring Boot的web项目。
先创建一个maven空工程,项目名为:spring-boot-hello-world2
新建完成之后需要我们自己来添加一些依赖和入口代码的编写,可以根据文档来编写
配置pom
<?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">
<artifactId>spring-boot-hello-world2</artifactId>
<groupId>com.thrstones</groupId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<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>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建 com.thrstones包;
在 com.thrstones包中创建HelloWorldApplication类;
package com.thrstones;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
创建com.thrstones.conrtoller包;
在 com.thrstones.conrtoller包中创建HelloWorldController类;
package com.thrstones.conrtoller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String sayHello(){
return "Hello Spring Boot2";
}
}
同样也是使用运行main方法的方式,让项目运行起来,输入http://localhost:8080/,来进行测试。