在真正的rest api服务还没有写好之前,为了方便前端测试调用,后端可以写个服务,伪造rest服务(写假数据)

1、官网: http://wiremock.org/

下载可执行jar:http://wiremock.org/docs/running-standalone/

2、java -jar启动服务

3、springboot的pom文件引入依赖

<!-- WireMock -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
</dependency> <!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

4、测试代码

package com.imooc.wiremock;

import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource; import com.github.tomakehurst.wiremock.client.WireMock; /**
* @author oy
* @date 2019年6月24日 下午11:13:12
* @version 1.0.0
*/
public class MockServer {
public static void main(String[] args) throws IOException {
WireMock.configureFor(8082);
WireMock.removeAllMappings(); WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo("/user/1")).willReturn(
WireMock.aResponse().withBody("{\"id\":1}").withStatus(200))); mock("/user/2", "01");
} private static void mock(String url, String file) throws IOException {
ClassPathResource resource = new ClassPathResource("mock/response/" + file + ".txt");
String content = StringUtils.join(FileUtils.readLines(resource.getFile(), "UTF-8").toArray(), "\n");
WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(url)).willReturn(WireMock.aResponse().withBody(content).withStatus(200)));
}
}

  

 测试结果:

  

  

使用WireMock伪造REST服务的更多相关文章

  1. 使用WireMock快速伪造RESTful服务

    ⒈下载WireMock独立运行程序 http://wiremock.org/docs/running-standalone/ ⒉运行 java -jar wiremock-standalone-2.2 ...

  2. wireMock快速伪造restful服务

    官网地址:http://wiremock.org/ Jar下载:http://repo1.maven.org/maven2/com/github/tomakehurst/wiremock/1.57/w ...

  3. 前后端分离中,使用swagger2和WireMock提高开发速度

    一.使用swagger2生成接口文档 依赖 <dependency> <groupId>io.springfox</groupId> <artifactId& ...

  4. 使用Spring MVC开发RESTful API(续)

    使用多线程提高REST服务性能 异步处理REST服务,提高服务器吞吐量 使用Runnable异步处理Rest服务 AsyncController.java @RestController @GetMa ...

  5. Weblogic服务端请求伪造漏洞(SSRF)和反射型跨站请求伪造漏洞(CSS)修复教程

    一.服务端请求伪造漏洞 服务端请求伪造(Server-Side Request Forgery),是指Web服务提供从用户指定的URL读取数据并展示功能又未对用户输入的URL进行过滤,导致攻击者可借助 ...

  6. 伪造服务钓鱼工具Ghost Phisher

    伪造服务钓鱼工具Ghost Phisher   Ghost Phisher是一款支持有线网络和无线网络的安全审计工具.它通过伪造服务的方式,来收集网络中的有用信息.它不仅可以伪造AP,还可以伪造DNS ...

  7. Spring Cloud Contract 微服务契约测试

    简介 使用场景 主要用于在微服务架构下做CDC(消费者驱动契约)测试.下图展示了多个微服务的调用,如果我们更改了一个模块要如何进行测试呢? 传统的两种测试思路 模拟生产环境部署所有的微服务,然后进行测 ...

  8. spring security入门

    1. Restful API和传统API的区别 用URL描述资源 用http描述方法行为,用http状态码描述结果 使用json交互数据 RESTful是一种风格,不是强制的标准 2. 使用sprin ...

  9. .Net中的AOP系列之《单元测试切面》

    返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle DynamicProxy测试 测试一个拦截器 注入依赖 ...

随机推荐

  1. CentOS8Linux中配置网易云网络yum源安装软件

    在CentOS8Linux中配置网易云网络yum源安装软件 前提是你的操作系统是CentOS-Linux 你已经配置好了本地yum源,并且你的网络是可用的. 本地yum源配置请参考:https://w ...

  2. 联盟周赛2019810 csgo (动态规划、不下降子序列)

    今天起晚了...就做了俩题 难受的一批... 题目描述 著名第一人称射击游戏 csgo 因其优秀的平衡性,爽快的射击感和科学的战术配比赢得了世界广大玩家的好评. 在一局游戏中,分为两个阵营,他们的目标 ...

  3. [转帖] Linux 下面 perl 命令的简介

    https://www.jb51.net/article/123326.htm 感觉挺好的 改天需要仔细学习一下. 前言 本文主要给大家介绍了关于Perl单行命令的相关内容,分享出来供大家参考学习,下 ...

  4. 洛谷 P5239 回忆京都 题解

    题面 裸的杨辉三角前缀和,但----- 在求前缀和的时候有可能得到一个负数(由于取模的原因),所以一定要加上模数后再取模!!!! #include <bits/stdc++.h> #def ...

  5. mysql常用的基本命令

    一.基本命令 1.启动服务(以管理员身份进入cmd): 格式:net start 服务名称 示例:net start mysql 2.停止服务(以管理员身份进入cmd): 格式:net stop 服务 ...

  6. 部署SonarQube代码检测服务并结合Jenkins使用

    一.SonarQube部署前的内核参数等配置以及java环境配置 1. 修改内核参数配置,使满足环境要求 [root@sonarqube ~]# vim /etc/sysctl.conf vm.max ...

  7. 简单了解 node http(一)

    简单了解 node http 模块 文章记录了对http 模块的简单使用与理解. http 服务端 http 客户端 总结 1. http 服务端 先写个小例子 服务端: let http = req ...

  8. js 学习二 字符串常用方法

    1.字符串长度 string.length var browserType = 'mozilla'; browserType.length; //7 2在字符串中查找子字符串 string.index ...

  9. 84. Largest Rectangle in Histogram (JAVA)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  10. Centos7安装Python3的方法[转]

    Centos7安装Python3的方法   由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_ ...