Spring Boot -- RESTFul API 简单项目的快速搭建
2018-03-05 23:36
856 查看
创建项目
参照 http://blog.csdn.net/oktfolio/article/details/79451766sring-boot-starter-web 引入的依赖包
引入 spring-tool 依赖
修改代码,自动编译运行。<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
需要修改 IntelliJ IDEA 的配置。
勾选 Build project automatically
Ctrl + Shift + Alt + /
Registry
勾选 compiler.automake.allow.when.app.running
运行
直接运行 main 方法、IntelliJ IDEA 运行,或者使用 maven 命令:spring-boot:runCode
IndexController.javapackage me.oktfolio.springboot.demo_2_1.controller; import me.oktfolio.springboot.demo_2_1.bean.User; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; @RestController // 包含 @ResponseBody @RequestMapping(value = "/index") public class IndexController { @RequestMapping public String index() { return "hello world"; } @RequestMapping(value = "/get") public Map<String, String> get(@RequestParam String name) { Map<String, String> map = new HashMap<>(); map.put("name", name); map.put("value", "hello world"); return map; } @RequestMapping(value = "/find/{id}/{name}") public User get(@PathVariable("id") Integer id, @PathVariable(value = "name") String name) { User user = new User(); user.setId(id); user.setUserName(name); user.setLocalDateTime(LocalDateTime.now()); return user; } }
User.java
package me.oktfolio.springboot.demo_2_1.bean; import java.time.LocalDateTime; public class User { private Integer id; private String userName; private LocalDateTime localDateTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public LocalDateTime getLocalDateTime() { return localDateTime; } public void setLocalDateTime(LocalDateTime localDateTime) { this.localDateTime = localDateTime; } }
测试类
package me.oktfolio.springboot.demo_2_1; import me.oktfolio.springboot.demo_2_1.controller.IndexController; 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.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @SpringBootTest public class Demo21ApplicationTests { private MockMvc mockmvc; @Before public void before() { this.mockmvc = MockMvcBuilders.standaloneSetup(new IndexController()).build(); } @Test public void contextLoads() throws Exception { RequestBuilder requestBuilder = get("/index"); mockmvc.perform(requestBuilder) .andExpect(status().isOk()) .andExpect(content().string("hello world")); } }
相关文章推荐
- Spring Boot-RESTfull API简单项目的快速搭建
- 003,spring boot RESTfull API简单项目的快速搭建
- 【SpringBoot教程二】:RESTfull API简单项目的快速搭建
- spring-boot(2) - RESTfull API简单项目的快速搭建
- 快速搭建一个restful风格的springboot项目
- springboot (1) 快速搭建简单项目
- 快速搭建一个简单的springboot项目
- springboot入门,简单能跑的项目快速搭建
- start.spring.io快速搭建springboot项目页面解析介绍
- IDEA-------(二)快速创建一个简单的Spring boot项目
- Spring Boot 用maven搭建RESTful项目
- Spring Boot 之 RESTfull API 项目搭建
- 【SpringBoot探索一】SpringBoot快速搭建基础web项目
- IDEA 快速将spring boot项目打包成jar包,简单快速有效
- 用Spring Boot搭建简单web项目
- 教你十分钟快速搭建springBoot项目实战
- springBoot快速搭建项目
- [置顶] WEB开发----Spring Boot项目快速搭建
- 快速搭建springboot项目并整合easymybatis
- 【SpringBoot】Spring Boot热部署(第5章 Spring Boot简单介绍 第6章 Spring Boot项目的搭建)