实现简单Restful API
1. 首选我们通过 http://start.spring.io/ 网址生成一个基础spring boot 项目,截图配置如下:

点击 generate Project 按钮生成并下载基础项目
2. 将下载的项目导入到 Intellij Idea 中,截图如下:

Intellij Idea 会自动添加maven依赖
3. 创建 HelloController.java 控制器类
package com.net263; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by Administrator on 2017/5/9.
*/ @RestController
public class HelloController { @RequestMapping("/hello")
public String index(){
return "Hello World";
}
4. 编写单元测试类
package com.net263; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
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 = HelloworldApplication.class)
@WebAppConfiguration
public class HelloworldApplicationTests { // 模拟调用Controller接口发起请求
private MockMvc mvc; // 初始化mvc
@Before
public void setUp(){
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
} // 测试hello 接口
@Test
public void testHello() throws Exception{
mvc.perform(MockMvcRequestBuilders.get("/hello").
accept(MediaType.APPLICATION_JSON)).
andExpect(status().isOk()).
andExpect(content().string(equalTo("Hello World")));
} }
5. 启动Srping boot 应用程序可以通过 http://localhost:8080/hello 请求访问到我们的Hello Restful API,截图如下:

6. 运行单元测试类可做单元测试:

实现简单Restful API的更多相关文章
- 使用 Express 和 waterline 创建简单 Restful API
这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000004996659 突然想起来我这个博客冷落了好多年了,也该更新一下, ...
- 使用ASP.NET Core构建RESTful API的技术指南
译者荐语:利用周末的时间,本人拜读了长沙.NET技术社区翻译的技术标准<微软RESTFul API指南>,打算按照步骤写一个完整的教程,后来无意中看到了这篇文章,与我要写的主题有不少相似之 ...
- 拿nodejs快速搭建简单Oauth认证和restful API server攻略
拿nodejs快速搭建简单Oauth认证和restful API server攻略:http://blog.csdn.net/zhaoweitco/article/details/21708955 最 ...
- MongoDB最简单的入门教程之五-通过Restful API访问MongoDB
通过前面四篇的学习,我们已经在本地安装了一个MongoDB数据库,并且通过一个简单的Spring boot应用的单元测试,插入了几条记录到MongoDB中,并通过MongoDB Compass查看到了 ...
- 简单说说Restful API
前言: 最近一段时间,一直在低头敲代码,开发平台对外交互的API接口,功能已经大体完成了,回过头来看看自己的接口设计文档,不胜感慨,想当初自己也是为"接口名称"想破了脑袋,各种百度 ...
- Java框架spring Boot学习笔记(九):一个简单的RESTful API
RESTful API设计需求如下: User.java package com.springboot.test; public class User { private Long id; priva ...
- 转:一个Restful Api的访问控制方法(简单版)
最近在做的两个项目,都需要使用Restful Api,接口的安全性和访问控制便成为一个问题,看了一下别家的API访问控制办法. 新浪的API访问控制使用的是AccessToken,有两种方式来使用该A ...
- (转载) RESTful API 设计指南
作者: 阮一峰 日期: 2014年5月22日 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制 ...
- Node.js实现RESTful api,express or koa?
文章导读: 一.what's RESTful API 二.Express RESTful API 三.KOA RESTful API 四.express还是koa? 五.参考资料 一.what's R ...
随机推荐
- 【254】◀▶IEW-Unit19
Unit 19 Technology Communication I.名词性从句在雅思写作中的运用 英语中哪些位置可以放名词? 1)主语 2)宾语 3)表语 4)同位语 名词的位置放一个句子=名词性从 ...
- ansible一键部署LAMP
一.实现ansible跟节点间无密码访问,不会配置的请看 文章 . 二.创建目录 $ mkdir -p playbooks/{files,templates} 三.创建php测试文件index.p ...
- OpenGL 使用GLFW创建全屏窗口
OpenGL 使用GLFW创建全屏窗口 GLFW库里面的glfwCreateWindow()函数是用来创建窗口的函数. 这样函数的原型是: GLFWwindow* glfwCreateWindow(i ...
- Entity Framework Code-First(15):Cascade Delete
Cascade Delete in Entity Framework Code-First: Cascade delete automatically deletes dependent record ...
- 32.我的wafBypass之道
0x01 搞起 当我们遇到一个waf时,要确定是什么类型的?先来看看主流的这些waf,狗.盾.神. 锁.宝.卫士等等...(在测试时不要只在官网测试,因为存在版本差异导致规则库并不一致) 1.云waf ...
- HDU 5242 Game (贪心)
题意:给定一棵树,要求从根结点1走k次,每次都是到叶子结点结束,把走过的所有的结点权值加起来,最大是多少. 析:先把每个结点到根结点的路径之和求出来,然后按权值从大到小排序,然后每次把路径中的权值求出 ...
- CSS中position的absolute和relative用法
static: HTML元素的默认定位方式 absolute: 将对象从文档流中拖出,使用left,right,top,bottom等属性进行绝对定位.而其层叠通过z-index属性定义.绝对定位的元 ...
- Cactus详细讲解
Cactus建议一年以上有经验的人玩,刚入门MVC,不了解下列组件请先自行学习,切勿好高骛远. Cactus的组成(基于.net4.5.2):Dapper+MVC4+autofac 前端css框架pu ...
- C# BackgroundWorker(异步线程)
日期:2018年11月28日 环境:Windows 10,VS2015 前言 .NET 类库中提供了一个快捷使用多线程的帮助类BackgroundWorker,能够快速创建一个新的线程,并能报告进度 ...
- OC官方文档翻译-Working-with-Protocols-协议的使用
查看全部文档翻译,请浏览https://github.com/L1l1thLY/Programming-with-Objective-C-in-Chinese,blog仅收录本人翻译的两章. 简述 在 ...