Eclipse 创建第一个 springboot 应用

时间:2021-03-03 08:04:08

1、前言

一直想把笔记整理出来,分享一下 springboot 的搭建;

因为私下 idea 用的比较多,使用比较方便,但恰逢小伙伴问起 eclipse 怎么搭建的问题, 顾整理以记之。

2、springboot 概述

搭建之前首先简单了解一下 springboot,他的出现就是为了解决 spring 企业级开发中大量臃肿的配置问题。

使用 springboot 可以做到专注于 spring 应用的开发,而无需过多的关注 XML 配置。

3、搭建项目

3.1、创建一个 maven 项目

打开 eclipse ,左上角  File > New > Others > Maven > Maven Project

注意:Create a simple project 不要勾选

Eclipse 创建第一个 springboot 应用

选择项目类型:

Eclipse 创建第一个 springboot 应用

填写项目参数:

Eclipse 创建第一个 springboot 应用

项目结构截图:

Eclipse 创建第一个 springboot 应用

Maven 项目创建后默认 JDK1.5:

只做补充了,就目前为止,所有 Maven 版本默认 JDK 版本都是 1.5,
至于修改也相对比较容易,可以通过 修改 Build Path 更改,或者是
修改 maven 目录下 conf 目录下 setting.xml 配置,可自行百度。

3.2、修改 pom.xml

如上,maven 项目创建完了,添加两处, 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>club.sscai</groupId>
<artifactId>spdemo</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>spdemo</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
</properties> <!-- 管理 springboot 中的版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!-- springboot web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

3.3、测试项目

如上几个步骤就已经搭建好了,改造一下 App.java

@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

创建一个 Controller 运行起来,HelloWorldController.java

@RestController
public class HelloWorldController { @GetMapping("/hello")
public String hello(){
return " hello Spring boot";
}
}

Eclipse 创建第一个 springboot 应用

运行 App.java 的 main() 方法,就像运行个 java 类一样:

Eclipse 创建第一个 springboot 应用

3.4、访问 hello

Eclipse 创建第一个 springboot 应用

4、总结

至此,一个 springboot 的 web 测试应用就搭建起来了,即便是使用 eclipse 也觉得很简单呢?

博客地址:http://www.cnblogs.com/niceyoo