springboot项目其实本质上是一个maven项目所以我们得先创建一个maven的web项目
1、创建一个maven项目
2、配置pom.xml
开启SpringBoot的两种方式
1)继承spring-boot-starter-parent项目
2)导入spring-boot-dependencies项目依赖SpringBoot开启的两种方式
<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.demo</groupId>
<artifactId>springboot2</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot2 Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 1.设置spring boot的parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 2.导入spring boot的web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 导入测试模块,包括JUnit、Hamcrest、Mockito。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- 3.添加Spring boot的插件 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、创建启动类
package com.demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@SpringBootApplication // Spring Boot项目的核心注解,主要目的是开启自动配置
@RestController // 默认类中的方法都会以json的格式返回
public class SpringbootApplication {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello world";
}
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
4、运行main方法启动springboot项目
下来说说集成过程中遇到的错误
***************************
APPLICATION FAILED TO START
***************************
Description:
The Bean Validation API is on the classpath but no implementation could be found
Action:
Add an implementation, such as Hibernate Validator, to the classpath
解决直接删掉maven本地仓库org/hibernate文件夹
快速构建springboot项目地址:https://start.spring.io/
下载导入就好了