Eclipse配置SpringBoot

时间:2023-03-09 08:27:28
Eclipse配置SpringBoot

从这一博客开始学习SpringBoot,今天学习Eclipse配置SpringBoot.Eclipse导入SpringBoot有两种方式,一种是在线一个是离线方式。

一、在线安装

点击Eclipse中的help->Eclipse Marketplace,搜索sts,如下图,可一直报错下面的错误,所以又下载离线包进行的安装。

Eclipse配置SpringBootEclipse配置SpringBoot

二、离线安装

开始的时候我下载的是springsource-tool-suite-3.9.4.RELEASE-e4.7.3a-updatesite这个版本。然后help->Install New Software->Add->Archive,这里由于我没解压所以选的是Archive,选择要安装的组件,点击Next.

Eclipse配置SpringBoot

三、使用SpringBoot创建项目

上面配置好之后可以使用SpringBoot来尝试着新建一个web项目。配置之后再New Project的页面会显示出Spring Boot选项,选择 Spring Starter Project。

Eclipse配置SpringBoot

配置项目名称,版本等信息。

Eclipse配置SpringBoot

这里选择创建web项目

Eclipse配置SpringBoot

创建完成之后项目的目录结构如下图

Eclipse配置SpringBoot

四、测试

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @RestController
//@Controller
//@ResponseBody
@RequestMapping("/sbs")
public class helloController { @RequestMapping("/hello")
public String Hello(){ return "Hello World";
}
}

Eclipse配置SpringBoot

这里需要注意上图的红色框的部分,这里使用的是@RestController,其实也可以使用下面注释的两个注解。点击CuiywTestApplication.java右键Run As->Spring Boot App启动。下图是启动成功的日志。

Eclipse配置SpringBoot

在浏览器输入http://localhost:8080/sbs/hello,可以看到下图返回的hello world.

Eclipse配置SpringBoot

五、出现的错误

如果启动springboot之后再次启动时就会报错,APPLICATION FAILED TO START, Failed to start connector [Connector[HTTP/1.1-8080]]

***************************
APPLICATION FAILED TO START
*************************** Description: The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured. Action: Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.

这里我直接通过任务管理器->用户,结束javaw:Java(TM) Platform SE binary这个任务。

Eclipse配置SpringBoot