Spring Boot 之 RESTfull API简单项目的快速搭建(一)
1.创建项目
2.创建 controller
IndexController.java
package com.roncoo.example.controller; import java.util.Date;
import java.util.HashMap;
import java.util.Map; 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 com.roncoo.example.bean.User; @RestController
@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<String, String>();
map.put("name", name);
map.put("value", "hello world"); return map;
} @RequestMapping(value = "find/{id}/{name}")
public User get(@PathVariable int id, @PathVariable String name) {
User u = new User();
u.setId(id);
u.setName(name);
u.setDate(new Date());
return u;
}
}
3.创建 bean -- 实体类 User
User.java
package com.roncoo.example.bean; import java.util.Date; public class User {
private int id;
private String name;
private Date date; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} }
4.测试
SpringBootDemo21ApplicationTests.java
package com.roncoo.example; 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; 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 com.roncoo.example.controller.IndexController; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemo21ApplicationTests { private MockMvc mvc; @Before
public void befor() {
this.mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
} @Test
public void contextLoads() throws Exception {
RequestBuilder req = get("/index");
mvc.perform(req).andExpect(status().isOk()).andExpect(content().string("hello world"));
} }
5.项目目录
Spring Boot 之 RESTfull API简单项目的快速搭建(一)的更多相关文章
- Spring Boot 之 RESTfull API简单项目的快速搭建(三)
1.运行打包后的项目 java -jar spring-boot-demo-2-1-0.0.1-SNAPSHOT.jar
- Spring Boot 之 RESTfull API简单项目的快速搭建(二)
1.打包 -- Maven build 2.问题 [WARNING] The requested profile "pom.xml" could not be activated ...
- 48. spring boot单元测试restfull API【从零开始学Spring Boot】
回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...
- spring boot 实现RESTFull API
- Spring Boot入门系列(二十)快速打造Restful API 接口
spring boot入门系列文章已经写到第二十篇,前面我们讲了spring boot的基础入门的内容,也介绍了spring boot 整合mybatis,整合redis.整合Thymeleaf 模板 ...
- 使用Spring Boot和Gradle创建AngularJS项目
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...
- Spring Boot微服务电商项目开发实战 --- 基础配置及搭建
根据SpringBoot实现分布式微服务项目近两年的开发经验,今天决定开始做SpringBoot实现分布式微服务项目的系列文章,帮助其他正在使用或计划使用SringBoot开发的小伙伴们.本次系列文章 ...
- 喜大普奔,两个开源的 Spring Boot + Vue 前后端分离项目可以在线体验了
折腾了一周的域名备案昨天终于搞定了. 松哥第一时间想到赶紧把微人事和 V 部落部署上去,我知道很多小伙伴已经等不及了. 1. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...
- 两个开源的 Spring Boot + Vue 前后端分离项目
折腾了一周的域名备案昨天终于搞定了. 松哥第一时间想到赶紧把微人事和 V 部落部署上去,我知道很多小伙伴已经等不及了. 1. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...
随机推荐
- Module ngx_http_v2_module
官方配置说明: http://nginx.org/en/docs/http/ngx_http_v2_module.html#example ngx_http_v2_module模块指令中文说明 ngx ...
- Java分布式系统高并发解决方案
对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了.而并发问题是绝大部分的程序员头疼的问题, 但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们一起来研 ...
- Android UI布局之LinearLayout
LinearLayout是Android中最经常使用的布局之中的一个.它将自己包括的子元素依照一个方向进行排列.方向有两种,水平或者竖直.这个方向能够通过设置android:orientation=& ...
- AsyncTask和Handler的优缺点
二者的区别我就不多说了,两个东西共同点都是为了解决耗时操作的问题,主要区别在于一个流程完善,拿来就用,一个偏向自主定制,扩展性高. 这里面有个谁是轻量级,谁适合大任务的问题:http://blog.s ...
- [转]专访企业QQ SaaS团队,谈企业级LNMP架构设计
FROM : http://www.csdn.net/article/2014-08-20/2821302-interview-tencent-b-qq-shuai-wang 对比IaaS和PaaS, ...
- 奇怪吸引子---Finance
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- javascript计算字符串长度
javascript计算字符串长度 学习了:https://blog.csdn.net/u012934325/article/details/75214847 function getByteLen( ...
- 转:Eclipse配色方案
http://www.cnblogs.com/arci/archive/2011/01/23/1942646.html 参考配色方案: http://www.cs.cmu.edu/~maverick/ ...
- 下周二推出“音视频技术WebRTC初探”公开课,欢迎捧场!
下周二推出"音视频技术WebRTC初探"公开课,欢迎捧场! 公开课课程链接:http://edu.csdn.net/huiyiCourse/detail/90 课程的解说资料 ...
- 为什么有的需要安全连接的的application只有开Fiddler才好用?
Help! Running Fiddler Fixes My App??? Over the years, the most interesting class of support reques ...