一基础项目构建,引入web模块,完成一个简单的RESTful API
Posted liandy001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一基础项目构建,引入web模块,完成一个简单的RESTful API相关的知识,希望对你有一定的参考价值。
一、Spring Boot的主要优点:
- 为所有Spring开发者更快的入门
- 开箱即用,提供各种默认配置来简化项目配置
- 内嵌式容器简化Web项目
- 没有冗余代码生成和XML配置的要求
二、使用maven构建项目
1.访问官网:http://start.spring.io/
选择基本的构建工具。
2.点击Generate Project
下载项目压缩包。
3.解压项目包,并用ecplise以Maven
项目导入
- 菜单中选择
File
–>New
–>Project from Existing Sources...
- 选择解压后的项目文件夹,点击
OK
- 点击
Import project from external model
并选择Maven
,点击Next
到底为止。 - 若你的环境有多个版本的JDK,注意到选择
Java SDK
的时候请选择Java 7
以上的版本
三、项目结构解析
通过上面步骤完成了基础项目的创建,Spring Boot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group所有差异):
src/main/java
下的程序入口:Chapter1Application
src/main/resources
下的配置文件:application.properties
src/test/
下的测试入口:Chapter1ApplicationTests
package com.didispace; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; @SpringBootApplication public class Chapter1Application extends SpringBootServletInitializer public static void main(String[] args) SpringApplication.run(Chapter1Application.class, args); @Override//为了打包springboot项目 protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) return builder.sources(this.getClass());
server.port=8080
package com.didispace; import com.didispace.web.HelloController; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.http.MediaType; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; 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; @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")));
生成的Chapter1Application
和Chapter1ApplicationTests
类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。
四、引入web模块
当前的pom.xml
内容如下,仅引入了两个模块:
spring-boot-starter
:核心模块,包括自动配置支持、日志和YAMLspring-boot-starter-test
:测试模块,包括JUnit、Hamcrest、Mockito
<?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> <groupId>com.didispace</groupId> <artifactId>Chapter1</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>Chapter1</name> <description>The first Spring Boot project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <defaultGoal>compile</defaultGoal> <sourceDirectory>src</sourceDirectory> <finalName>springboot-package</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.didispace.Chapter1Application</mainClass> <skipTests>true</skipTests> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
五、编写服务
package com.didispace.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController @RequestMapping("/hello") public String index() return "Hello World";
启动主程序,打开浏览器访问http://localhost:8080/hello
,可以看到页面输出Hello World
六、编写单元测试用例
package com.didispace; import com.didispace.web.HelloController; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.http.MediaType; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; 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; @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")));
以上是关于一基础项目构建,引入web模块,完成一个简单的RESTful API的主要内容,如果未能解决你的问题,请参考以下文章