java .stub 用法_SpringMVC 使用stub层

使用前提:

接口不存在(即报404)时候使用stub假数据.

入口:

BusinessExceptionHandlerAdvice中

NoHandlerFoundException

异常捕获方法中.

流程图

50dac3d11b422160285ec622abb41c5c.png

配置文件是在哪里读取的?

在oa/web/controller/handler/EnvironmentInit.java 中,

EnvironmentInit 实现了ApplicationListener,

所以在tomcat应用启动成功后就会执行,

在onApplicationEvent 方法中读取配置文件,

配置文件路径:

"/config/stubMap.json"

EnvironmentInit源码:

package oa.web.controller.handler;

import com.file.hw.props.GenericReadPropsUtil;

import com.io.hw.json.HWJacksonUtils;

import com.string.widget.util.RegexUtil;

import com.string.widget.util.ValueWidget;

import org.springframework.context.ApplicationListener;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.event.ContextRefreshedEvent;

import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

import java.util.Map;

/**

* Created by 黄威 on 3月/22/18.

* 读取配置文件的核心类,是获取properties参数的唯一入口

* 执行顺序

* 注解Resource的setter方法

* 实现接口ServletContextAware的setServletContext 方法

* onApplicationEvent方法

* config/stubMap.json 的格式:

*

* {

* "/a/b/c": "a_b_c",

* "/house/recommended/listfilter/json": "recommendedList",

* "ccc": "ddd",

* "ccc": "ddd",

* "test": "test"

* }

*

* key 是接口路径,建议以斜杠开头,

* value 是redis 的key

* 如何设置stub response 内容?:

* 通过接口 :http://127.0.0.1:8080/redis/setCache,传递参数:id,key,value

* 配置文件路径:

* /src/main/resources/config/stubMap.json

*/

@Configuration

public class EnvironmentInit implements ApplicationListener, ServletContextAware {

private ServletContext servletContext;

private Map stubPathMap;

// @Resource

// private RequestSafeThreadParamDto requestSafeThreadParamDto;

@Override

public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

if (!ValueWidget.isNullOrEmpty(getStubPathMap())) {

return;

}

ClassLoader classLoader = this.servletContext.getClassLoader();

String resourcePath = "/config/stubMap.json";

String json = GenericReadPropsUtil.getConfigTxt(classLoader, resourcePath);

if (null != json) {

json = RegexUtil.sedDeleteComment(json);//删除第一行的注释

setStubPathMap(HWJacksonUtils.deSerializeMap(json, String.class));

}

}

@Override

public void setServletContext(ServletContext servletContext) {

this.servletContext = servletContext;

}

public Map getStubPathMap() {

return stubPathMap;

}

public void setStubPathMap(Map stubPathMap) {

this.stubPathMap = stubPathMap;

}

}

stub假数据存储在哪里

存储在redis 中,

那么如何通过接口路径获取redis key呢?

通过如下方法:

String redisKey = servletActionMap.get(path);

if (null == redisKey && path.startsWith("/")) {//比如如果找不着"/agent/list/json",则去查找"agent/list/json"

redisKey = servletActionMap.get(path.substring(1, path.length()));

}

if (null == redisKey) {//比如如果找不着"agent/list/json",则找"agent_list_json"

redisKey = path.replace("/", "_");

if (redisKey.startsWith("_")) {

redisKey.substring(1, redisKey.length());

}

}

说明:上述代码中的path参数,是

request.getServletPath()

比如接口 路径是"/agent/list/json",

那么拿"/agent/list/json"作为map的key,从映射的map 中查找,

若查找不到,则把

"agent_list_json"

作为redis key

映射的map从哪里获取?

从两个地方获取:

1.本地配置文件(优先级低):

"/config/stubMap.json"

内容如下(示例):

/*** 将被oa/web/controller/handler/EnvironmentInit.java 调用**/

{

"/a/b/c": "a_b_c",

"house/recommended/listfilter/json": "recommendedList",

"ccc": "ddd",

"ccc": "ddd",

"test": "test"

}

2,从redis中获取,优先级高,所以会覆盖本地文件配置

注意:

1.redis中的map比"/config/stubMap.json" 优先级高;

2,如果判断从redis获取的stub数据为空,则仍然继续走正常流程.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值