1. 生成项目
bash
mvn archetype:generate
填写groupId和artifactId,其余默认即可
2. 修改pom.xml文件
将如下内容放入pom.xml文件内
xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/>
</parent>
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3. 新建目录及文件
- 新建controller文件
在如下目录新建controller目录,src\main\java\io\liuzhifei - 新建HelloController.java
在src\main\java\io\liuzhifei\controller文件夹下创建HelloController.java文件,并写入以下内容
java
package io.liuzhifei.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello()
{
return "Hello Spring World!!!!!!!!!!!!";
}
}
- 新建HelloApplication.java
在src\main\java\io\liuzhifei文件夹下创建HelloApplication.java,并写入如下代码
java
package io.liuzhifei;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
4. 运行
- 法一
命令行输入
bash
mvn spring-boot:run
浏览器输入http://localhost:8080/hello进行查看
- 法二
pom.xml写入如下内容
xml
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>io.liuzhifei.HelloApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
命令行执行
bash
mvn exec:java -Dexec.mainClass="io.liuzhifei.HelloApplication"