spring-cloud教程一:创建spring boot
背景
想想,微服务
这概念在当初刚从业时就听过,那时也只是停留在概念上,缺少技术支撑,或者说没有公认完美的技术支撑。docker
的出现,给微服务提供了平台支持,spring cloud
的出现给微服务提供全家桶
式的解决方案,几乎成了微服务的代名词。
微服务需要解决的问题太多太多,既然spring cloud
号称是一站式的解决方案,对微服务中碰到的问题都提供相应的解决方案,因此spring cloud
有品目繁多的项目,截止到目前,官网上列的就有24
个项目,每个项目都用于解决特定的问题。
其中spring boot
应该是最核心的一个组件,用于具体业务逻辑的实现 。本文以一个简单的接口根据用户工号获取用户信息
为例,介绍spring boot
的使用。
创建spring boot工程
- 打开https://start.spring.io/生成一个标准
spring boot
工程. 因为需要restful支持,Dependencies需要输入Web
,提供对web的支持。 - 创建
IntelliJ IDEA
项目,选择支持Maven
. - 将压缩包中
src
目录覆盖项目src
目录 - 将项目
pom.xml
替换为压缩包中的pom.xml
文件。 - 在
pom.xml
文件上右键选择Maven->Reimport
导入相应的jar包。
目录结构
. ├── ./pom.xml ├── ./springboot.iml └── ./src ├── ./src/main │ ├── ./src/main/java │ │ └── ./src/main/java/yaya │ │ └── ./src/main/java/yaya/demo │ │ └── ./src/main/java/yaya/demo/springboot │ │ └── ./src/main/java/yaya/demo/springboot/SpringbootApplication.java │ └── ./src/main/resources │ ├── ./src/main/resources/application.properties │ ├── ./src/main/resources/static │ └── ./src/main/resources/templates └── ./src/test └── ./src/test/java └── ./src/test/java/yaya └── ./src/test/java/yaya/demo └── ./src/test/java/yaya/demo/springboot └── ./src/test/java/yaya/demo/springboot/SpringbootApplicationTests.java
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>yaya.demo</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建controller
1. 创建用户类yaya.demo.springboot.pojos.User
package yaya.demo.springboot.pojos; /** * @Description: * @author: jianfeng.zheng * @since: 2018/8/14 下午7:48 * @history: 1.2018/8/14 created by jianfeng.zheng */ public class User { private String username; private String uid; private String email; //...getter and setter }
2. 创建controlleryaya.demo.springboot.controller.UserController
package yaya.demo.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import yaya.demo.springboot.pojos.User; /** * @Description: * @author: jianfeng.zheng * @since: 2018/8/14 下午7:45 * @history: 1.2018/8/14 created by jianfeng.zheng */ @RestController public class UserController { @RequestMapping(value = "/user",method = RequestMethod.GET) public User getUserInfo(@RequestParam(name = "uid")String uid){ User user=new User(); user.setEmail("[email protected]"); user.setUsername("jianfeng.zheng"); user.setUid(uid); return user; } }
3. 运行启动类SpringbootApplication
4. 测试
# curl http://localhost:8080/user?uid=004 {"username":"jianfeng.zheng","uid":"004","email":"[email protected]"}
单元测试
修改单元测试类SpringbootApplicationTests
package yaya.demo.springboot; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; 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 yaya.demo.springboot.controller.UserController; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest() @WebAppConfiguration public class SpringbootApplicationTests { private MockMvc mvc; @Before public void init() { mvc = MockMvcBuilders.standaloneSetup(new UserController()).build(); } @Test public void getUserInfo() throws Exception { mvc.perform((MockMvcRequestBuilders.get("/user") .param("uid", "004"))) .andDo(print()) .andExpect(status().isOk()); } }
打包运行
- 在工程目录下执行命令
mvn -Dmaven.test.skip=true install
- 运行
target
文件夹下生成可运行jar包.
java -jar springboot-0.0.1-SNAPSHOT.jar
相关推荐
与卿画眉共浮生 2020-11-13
CaesarHome 2020-11-09
瑞风轻拂 2020-11-02
hellowordmonkey 2020-11-02
丽丽 2020-10-30
流年浅滩 2020-10-23
feinifi 2020-10-14
yangjinpingc 2020-10-09
RickyIT 2020-09-27
lisongchuang 2020-09-27
permanent00 2020-09-15
李玉志 2020-08-19
spring 2020-08-18
meleto 2020-08-17
幸运小侯子 2020-08-14
85407718 2020-08-09
csuzxm000 2020-08-02
咻pur慢 2020-08-02
libowenhit 2020-07-29