Eclipse中如何创建一个新的SpringBoot项目及问题解
2019-01-12 本文已影响0人
周山
如何在Eclipse中创建一个新的spring Boot项目,Eclipse没有sts插件
1.安装STS插件
首先在Eclipse中安装STS插件
在Eclipse主窗口中点击 Help -> Eclipse Marketplace...
Search或选择“Popular”标签,选择Spring Tool 插件,点击Install:

2.安装成功之后,重启
New --> Project...
找到Spring Boot目录,选择Spring Starter Project,Next

3.填写配置信息
我这里项目名叫demo-1,未做修改直接next

4.选择版本和组件(自行检索添加)
笔者选了2.1.2版本,选了mysql和web,点击 Finish ,就会有一个新项目,第一次需要等待几分钟,sts工具会生成spring boot目录的结构及文件

5.项目结构生成完毕

6.pom.xml文件
里面有两个依赖,就是刚刚我们选择的web和mysql, 这里就加入了对应的依赖包,需要添加其他依赖和编译插件在这里配置即可。
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
7.创建Test类
@RestController
public class TestController {
@RequestMapping("/hello1")
public String test(){
return "hello,this is a springboot demo";
}
}
8.设置tomcat端口号
在application.properties文件中设置tomcat端口号为8081
server.port=8081
9. 启动项目
右键DemoApplication中的main方法,Run As -> Spring Boot App,项目就可以启动了。

10.访问页面
http://localhost:8081/hello

ps:在安装过程中,出现了以下问题

网上找了一些关于解决这个问题的博客,发现这两个比较靠谱值得一试(对于这个报错,只需要看第二段的问题就是图片圈起来的位置,然后复制第二段的百度)
https://blog.csdn.net/layman1024/article/details/77418414
https://blog.csdn.net/qq_30264689/article/details/79716354
如果不行在每一种方法做完后重启下电脑再重新安装。