上一篇文章已经使用Maven 搭建了一个WEB项目。
现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来。
一、在pom.xml中引入spring-boot-start-parent,spring官方的叫stater poms,
它可以提供dependency management,也就是依赖管理,引入以后在声明其它dependency的时候就不需要version了。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
二、需要在pom.xml中引入spring-boot-starter-web,spring官方解释spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
三、如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven的spring-boot:run的话就不需要此配置。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
四、下面开始写程序,我们需要一个启动类,然后在启动类声明让spring boot自动给我们配置spring需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过浏览器访问hello world字样的例子:
package com.whf.chen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:辰
* @E-mail:15538323378@163.com
* @CSDN博客:http://blog.csdn.net/chenfengbao
* @2018年2月22日下午4:18:21
*/
@SpringBootApplication
@RestController
public class SpringBootHelloWorld {
@RequestMapping("/chen")
public String hello() {
return "Hello Spring Boot!";
}
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorld.class, args);
}
}
其中@SpringBootApplication声明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。
@RestController返回json字符串的数据,直接可以编写RESTFul的接口。
五、运行我们的Application,我们先介绍第一种运行方式。
第一种方式特别简单:右键Run As -> Java Application。之后打开浏览器输入地址:http://127.0.0.1:8080/chen 就可以看到Hello Spring Boot!了。
第二种方式右键project – Run as – Maven build – 在Goals里输入spring-boot:run ,然后Apply,最后点击Run。
六、完整的pom.xml文件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.whf.chen</groupId>
<artifactId>springboot_helloworld</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_helloworld Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springboot_helloworld</finalName>
</build>
</project>
至些,我们的Spring boot项目已经搭建完成,并成功的运行了HelloWorld的项目。