基于反射的路由、网关
程序员文章站
2022-03-10 23:13:38
...
控制层对外的前缀是统一的,不同的service配不同的方法,通过反射跳转到不同的实现类上,此处仅含controller 代码
CallBackWalletController
package com.gateway.controller;
import com.alibaba.fastjson.JSONObject;
import com.bitfty.gateway.config.SpringContextUtil;
import com.bitfty.gateway.exception.ApiRuntimeException;
import com.bitfty.gateway.util.InfoCode;
import com.bitfty.gateway.util.RestResp;
import com.bitfty.user.service.BaseBusiness;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
/**
* @author :liujipeng
* @date :Created in 2019/11/20 17:49
* @description:${description}
*/
@RestController
@RequestMapping("api/v1/callBackWallet")
public class CallBackWalletController {
protected HttpServletRequest request;
protected HttpServletResponse response;
/**
* @param methodName
*/
@RequestMapping("/{businessName}/{methodName}")
public void executeAPI(@PathVariable String businessName, @PathVariable String methodName) throws Exception {
RestResp restResp = new RestResp();
Object invoke = null;
String decrypt_str = null;
try {
ServletInputStream inputStream = request.getInputStream();
decrypt_str = getStreamString(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObject = JSONObject.parseObject(decrypt_str);
businessName = businessName + "ServiceImpl";
BaseBusiness business = (BaseBusiness)SpringContextUtil.getBean(businessName);
Method method = business.getClass().getMethod(methodName, String.class);
invoke = method.invoke(business, JSONObject.toJSONString(jsonObject));
response.setStatus(200);
//ajaxResponse("OK");
ajaxResponse(JSONObject.toJSONString(invoke));
}
public void ajaxResponse(String res) {
try {
this.response.setCharacterEncoding("UTF-8");
this.response.setContentType("text/html; charset=UTF-8");
this.response.getWriter().print(res);
this.response.getWriter().close();
} catch (IOException var3) {
new ApiRuntimeException(InfoCode.INTERNAL_ERROR, "Response输出异常");
}
}
static String getStreamString(InputStream inputStream) {
if (inputStream != null) {
try {
BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuffer tStringBuffer = new StringBuffer();
new String("");
String sTempOneLine;
while((sTempOneLine = tBufferedReader.readLine()) != null) {
tStringBuffer.append(sTempOneLine);
}
inputStream.close();
return tStringBuffer.toString();
} catch (Exception var4) {
var4.printStackTrace();
}
}
return null;
}
@ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}
}
SpringContextUtil
package com.gateway.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author :
* @date :Created in 2019/11/22 15:21
* @description:${description}
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
public static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName){
return applicationContext.getBean(beanName);
}
}
BaseBusiness
package com.user.service;
/**
* @author :
* @date :Created in 2019/11/25 15:17
* @description:${description}
*/
public interface BaseBusiness {
}
WalletcCallBackService
package com.user.service;
/**
* @author :
* @date :Created in 2019/11/21 16:08
* @description:${description}
*/
public interface WalletcCallBackService extends BaseBusiness{
public String transCallBck(String body);
public Boolean RechargeCallback(String body);
}
这样一个通用网关就配置好了,只需要通过/{businessName}跳转不同的service ,不同的{methodName} 走不同 的方法,只不过这样的service方法 均只能接受一个json串,需要用逻辑将json解析成需要的属性值
推荐阅读
-
基于vue-router 多级路由redirect 重定向的问题
-
防火墙基于策略路由的配置技巧
-
vue路由导航守卫和请求拦截以及基于node的token认证的方法
-
详解Spring Cloud Gateway基于服务发现的默认路由规则
-
AngularJS基于ui-route实现深层路由的方法【路由嵌套】
-
基于Nacos实现Spring Cloud Gateway实现动态路由的方法
-
详解基于angular路由的requireJs按需加载js
-
基于express中路由规则及获取请求参数的方法
-
(springCloud-8 Zuul网关路由的基本配置feign实现接口调用)
-
SpringCloud-使用路由网关的服务过滤功能-拦截登录前是否有token为例