Eclipse或者IDEA安装Maven的过程这里就不详细赘述了,Eclipse可以参考《Eclipse配置Maven和Spring Boot》,IDEA可以参考《使用IntelliJ IDEA 配置Maven》
使用IDE创建新的项目,选择Maven项目,这里不适用骨架

点击Next,输入GroupId与ArtifactId

点击Next,输入项目名和项目存放路径名

点击Finish完成创建

首先指定父级依赖,将如下配置添加到project标签中
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--SpringBoot版本-->
<version>1.5.9.RELEASE</version>
</parent>然后添加spring-web启动器依赖到dependencies标签中,没有该标签的在project标签下添加一个即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>最终配置文件如下:
<?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>cn.doubly</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--指定父级依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!--添加依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>Controller包,创建一个HelloWorldController控制器@Controller给Spring标明这是一个控制器@EnableAutoConfigration在类的头上让SpringBoot自动配置它@RequestMapping("/hello")和@ResponseBody注解以上基本是Spring的写法,不熟悉的建议先学习Spring
目录结构如下图

最终代码如下:
package Controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@EnableAutoConfiguration
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){ return "Hello World";
}
}
@SpringBootApplication注解在类的上面SpringApplication.run(SpringBootDemo.class,args); 最终代码如下
package cn.doubly;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemo {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo.class,args);
}
}至此,springBoot项目就搭建完成,点击启动就可以访问。

访问成功
目录结构如下


重启生效
