案例创建一个新的项目并测试启动
此时项目结构如下,可能pom.xml文件会报错
这是因为spring boot 2.1.8的maven插件与eclipse不兼容。这时需要在pom.xml里面添加一行,指定maven插件的版本,如下:
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version><!-- 加入这行避免报错 -->
</properties>
这时更新项目即可(Maven-Update Project),没有报错信息
整个的pom.xml文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>cn.tedu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version><!-- 加入这行避免报错 -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
此时整个项目结构如下:
启动springBoot:打开SpringDemoApplication.java文件,Run As --Spring Boot App,显示如下页面:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE)
2019-09-18 11:49:42.538 INFO 18784 --- [ main] cn.tedu.SpringDemoApplication : Starting SpringDemoApplication on LAPTOP-JLA8JBR3 with PID 18784 (E:\workspace\spring_demo\target\classes started by wq in E:\workspace\spring_demo)
2019-09-18 11:49:42.541 INFO 18784 --- [ main] cn.tedu.SpringDemoApplication : No active profile set, falling back to default profiles: default
2019-09-18 11:49:43.709 INFO 18784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-18 11:49:43.757 INFO 18784 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-18 11:49:43.758 INFO 18784 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-18 11:49:43.943 INFO 18784 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-18 11:49:43.943 INFO 18784 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1334 ms
2019-09-18 11:49:44.220 INFO 18784 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-18 11:49:44.427 INFO 18784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-18 11:49:44.431 INFO 18784 --- [ main] cn.tedu.SpringDemoApplication : Started SpringDemoApplication in 2.214 seconds (JVM running for 3.112)
可以看到,端口号为8080,
这是写一个测试HelloWord,注意:
其余自己写的都必须在SpringDemoApplication.java文件的子包下面,也就是说,其他的包,需要是application文件所在包的子包
测试类如下:
package cn.tedu.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello(){
return "hello World";
}
}
包结构如下:
此时先关闭之前的启动,再次启动,否则会报出出端口被占用的问题,再次Run As --Spring Boot App
此时浏览器显示:
这时一个SpringBoot项目就搭建好了