纵有疾风起
人生不言弃

使用Intellij中的Spring Initializr来快速构建Spring Boot工程 完成一个简单的RESTful API

文章目录

1 Spring Boot概述

本文主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。

Spring Boot工程的创建。而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也可以通过《Spring Boot快速入门》一文中提到的SPRING INITIALIZR页面工具来创建。

本文我们将介绍嵌入Intellij IDEA中的Spring Initializr工具,它可以帮助我们快速的构建出一个基础的Spring Boot/Cloud工程。

2 使用IDEA中的Spring Initializr工具创建项目

2.1 New => Project

菜单栏中选择File=>New=>Project..,选择红框所示Spring Initializr类型的项目。我们可以看到如下图所示的创建功能窗口。

其中Initial Service Url指向的地址就是Spring官方提供的Spring Initializr工具地址,所以这里创建的工程实际上也是基于官网的Web工具来实现的。

使用Intellij中的Spring Initializr来快速构建Spring Boot工程 完成一个简单的RESTful API插图

点击Next,等待片刻后,我们可以看到如下图所示的工程信息窗口,在这里我们可以编辑我们想要创建的工程信息。

其中,Type可以改变我们要构建的工程类型,比如:Maven、Gradle;Language可以选择:Java、Groovy、Kotlin。

使用Intellij中的Spring Initializr来快速构建Spring Boot工程 完成一个简单的RESTful API插图1

点击Next,进入选择Spring Boot版本和依赖管理的窗口。

在这里值的我们关注的是,它不仅包含了Spring Boot Starter POMs中的各个依赖,还包含了Spring Cloud的各种依赖。

使用Intellij中的Spring Initializr来快速构建Spring Boot工程 完成一个简单的RESTful API插图2

点击Next,进入最后关于工程物理存储的一些细节。在这里可以修改项目的名字HelloSpring;最后,点击Finish就能完成工程的构建了。

使用Intellij中的Spring Initializr来快速构建Spring Boot工程 完成一个简单的RESTful API插图3

Intellij中的Spring Initializr虽然还是基于官方Web实现,但是通过工具来进行调用并直接将结果构建到我们的本地文件系统中,让整个构建流程变得更加顺畅。

2.2 项目代码结构解析

通过上面步骤完成了基础项目的创建,如上图所示,Spring Boot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group所有差异):

  • src/main/java下的程序入口:HelloSpringApplication
  • src/main/resources下的配置文件:application.properties
  • src/test/下的测试入口:HelloSpringApplicationTests

使用Intellij中的Spring Initializr来快速构建Spring Boot工程 完成一个简单的RESTful API插图4

生成的HelloSpringApplicationHelloSpringApplicationTests类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。

2.3 项目maven解析

由于项目是使用maven管理jar包,只需要简单配置根目录下的:pom.xml 即可管理所有的依赖jar包。

我们首先来看 已安装的jar包,打开pom.xml观察:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

我们发现当前项目仅引入了这两个模块:

  • spring-boot-starter:核心模块,包括自动配置支持、日志和YAML
  • spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito

3 实现简单的RESTful功能

3.1 添加web模块

为了完成一个简单的RESTful API的任务,我们需要在pom.xml中另外引入spring-boot-starter-web模块。

引入方式特别简单,只需要按照2.3节的形式,在pom.xml中加入一下四行代码即可:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

3.2 编写HelloWorld服务

创建package命名为 com.upul.web (根据实际情况修改)

注意:要在主程序 HelloSpringApplication的同一个包下面 创建一个子包

创建HelloController.java类内容如下:

@RestController
public class HelloController { 

    @RequestMapping("/hello")
    public String index() { 
        return "Hello World";
    }

}

启动主程序,打开浏览器访问http://localhost:8080/hello,可以看到页面输出Hello World

3.3 编写单元测试用例

打开的src/test/下的测试入口Chapter1ApplicationTests类。下面编写一个简单的单元测试来模拟http请求,具体如下:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class Chapter1ApplicationTests { 

	private MockMvc mvc;

	@Before
	public void setUp() throws Exception { 
		mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
	}

	@Test
	public void getHello() throws Exception { 
		mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
				.andExpect(status().isOk())
				.andExpect(content().string(equalTo("Hello World")));
	}

}

使用MockServletContext来构建一个空的WebApplicationContext,这样我们创建的HelloController就可以在@Before函数中创建并传递到MockMvcBuilders.standaloneSetup()函数中。

注意引入下面内容,让上述statuscontentequalTo函数可用

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

至此已完成目标,通过Maven构建了一个空白Spring Boot项目,再通过引入web模块实现了一个简单的请求处理。

参考:

参考:《Spring Boot快速入门》:http://blog.didispace.com/spring-boot-learning-1/

参考:《使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程》:http://blog.didispace.com/spring-initializr-in-intellij/

原文链接:https://lookme.blog.csdn.net/article/details/97750294

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » 使用Intellij中的Spring Initializr来快速构建Spring Boot工程 完成一个简单的RESTful API
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录