如何快速构建 Spring Boot 基础项目?
关注“Java 后端技术全栈”**
回复“面试”获取全套大厂面试资料
快速构建 Spring Boot 基础项目的两种方式:
spring 官方工具
IDEA 工具
spring 官方工具构建 Spring Boot 项目
浏览器上输入地址:https://start.spring.io/
如图所示

我这里使用的是 maven,语言选择 java。packaging 选择 Jar,JDK 版本选择自己的版本,我这里选择的 JDK1.8。
最后选择一个 SpringBoot 版本。
右边是添加依赖

这里我们选个 spring web,下载到本地

解压到自己的本地空间,然后使用 IDEA 导入,项目目录:

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/...d">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
项目其他都为空,application.properties 都是空白的。

为了演示,这里添加一个 Controller
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/hello")
public String hello(){
return "hello world" ;
}
}
启动 DemoApplication.java 中的 main 函数。

默认端口是 8080。
在浏览器上输入:http://localhost:8080/hello

以上便是使用 spring 官方提供的工具创建 Spring Boot 项目的一种方式。下面来使用 IDEA 创建 Spring Boot 项目。
IDEA 创建 Spring Boot 项目
Intellij IDEA 中的 Spring Initializr 工具
File-->new---project

选择 next,我们可以看到

Group 顾名思义就是你的公司名,一般是填写 com.*。
Artifact groupId 和 artifactId 是 maven 管理项目包时用作区分的字段,就像是地图上的坐标。这里填写项目名即可。
Type 就是构建的项目类型,意思就是你希望你的项目使用什么工具构建,可选 maven 和 gradle 一般选 maven。
Language 顾名思义就是你的项目用啥语言开发,可选 Java、Groovy、Kotlin
Packaging 就是你希望你的项目打成什么形式的包,可选 Jar、War SpringBoot 项目一般选 Jar
Java Version 意指项目使用的 java 版本,根据你的需要选择,我这里使用的是 8。
Version 项目的初始版本,默认即可。
Name 项目名称。
Description 项目描述,默认即可。
Package 包名,填完 Group 和 Artifact 后自动生成,默认即可。
继续 next

这里为了好演示,所以选择 web,这里是可以选择很多的,有空自行翻阅一下,一键式搞定依赖。也称约定大于配置。

next(因为前面已经有 demo,所以这里取个 demo1)

Finish.
导入到 IDEA 中

和前面使用 spring 官方工具构建的一毛一样。
然后继续把前面那个 Controller 搞过来

启动 DemoApplication

浏览器上输入:http://localhost:8080/hello

好了,以上便是今天分享的两周构建 spring boot 项目的方法。希望上面两种方式对你有所帮助。
码字不易,点个在看+分享,非常感谢!

版权声明: 本文为 InfoQ 作者【田维常】的原创文章。
原文链接:【http://xie.infoq.cn/article/ccd1bc42d9b14ddb7bdbc7f51】。文章转载请联系作者。
评论