@RequestMapping("{appName}/{uri}")
@VerifyLogin(LoginType.MUST)
Object executeByLogin(@PathVariable("appName") String appName,
@PathVariable("uri") String uri,
@RequestParam(required = false) Map<String, String> queryParams,
@RequestBody(required = false) Map<String, Object> jsonBody,
HttpServletRequest request,
HttpServletResponse response);
@Override
public Object executeByLogin(String appName, String uri, Map<String, String> queryParams, Map<String, Object> jsonBody,
HttpServletRequest request,
HttpServletResponse response) {
String domain = environment.getProperty(appName + ".domain");
String url = new StringBuffer(domain)
.append("/")
.append(uri.replace("_", "/"))
.toString();
String method = request.getMethod().toUpperCase();
Enumeration<String> headerNames = request.getHeaderNames();
Map<String, String> header = new HashMap<>();
while (headerNames.hasMoreElements()) {
String str = headerNames.nextElement();
header.put(str, request.getHeader(str));
}
Object result = null;
try {
switch (method) {
case "GET":
result = HttpUtility.get(url, queryParams, new HashMap<>());
break;
case "POST":
result = HttpUtility.postJson(url, new HashMap<>(),new HashMap<>(),jsonBody);
break;
}
} catch (Exception ex) {
switch (method) {
case "GET":
log.error("接口[{}]调用,参数:{},header:{},结果:{}",url, com.alibaba.fastjson.JSON.toJSONString(queryParams), com.alibaba.fastjson.JSON.toJSONString(header),result,ex);
break;
case "POST":
log.error("接口[{}]调用,参数:{},header:{},结果:{}",url, com.alibaba.fastjson.JSON.toJSONString(jsonBody), com.alibaba.fastjson.JSON.toJSONString(header),result,ex);
break;
}
}finally {
switch (method) {
case "GET":
log.info("接口[{}]调用,参数:{},header:{},结果:{}",url, com.alibaba.fastjson.JSON.toJSONString(queryParams), com.alibaba.fastjson.JSON.toJSONString(header),result);
break;
case "POST":
log.info("接口[{}]调用,参数:{},header:{},结果:{}",url, com.alibaba.fastjson.JSON.toJSONString(jsonBody), com.alibaba.fastjson.JSON.toJSONString(header),result);
break;
}
}
if (result == null) {
Response<Object> errorResponse = Response.ofFailure(-1, request.getRequestURI() + " : no api message available");
return errorResponse;
}
return result;
}
一个转发请求的方法
于 2024-11-01 15:31:31 首次发布