模拟应用网关下游系统的一些场景测试接口
场景1:构造一个返回请求参数(表单入参),请求header,设置响应header的测试demo接口
框架:springboot
@ResponseBody
@RequestMapping("/test/api/v1")
public Map serverPostTestv1(HttpServletRequest httpRequest, HttpServletResponse httpResponse,@RequestHeader MultiValueMap<String,String> reqHeaders){
Map map = new HashMap();
Map reqheadersmap = new HashMap();
Map reqparamsmap = new HashMap();
LOG.info("请求headers是:"+ reqHeaders);
//获取请求header
reqheadersmap.put("reqheaders",reqHeaders);
//获取请求参数
Enumeration paramNames = httpRequest.getParameterNames();
while(paramNames.hasMoreElements()){
String paramName = (String) paramNames.nextElement();
String[] paramValues = httpRequest.getParameterValues(paramName);
LOG.info("请求params是:"+ paramName);
if(paramValues.length >0){
String paramValue = paramValues[0];
if(paramValue.length() != 0){
LOG.info("reqparamsmap.put 是:"+ paramName+","+paramValue);
reqparamsmap.put(paramName,paramValue);
}
}
}
//设置响应headers
httpResponse.setHeaders("Content-Finished","999");
httpResponse.setHeaders("Content-Halo","togwtest");
//请求头,请求参数放进map
map.put("reqparams",reqparamsmap);
map.put("reqheaders",reqheadersmap);
return map;
}
场景2:构造一个返回请求参数(json入参)的测试demo接口
框架:springboot
@ResponseBody
@RequestMapping("/test/api/v2")
public JSONObject serverPostTestv2(HttpServletRequest httpRequest, HttpServletResponse httpResponse,@RequestHeader MultiValueMap<String,String> reqHeaders) IOException{
StringBuilder buffer = new StringBuilder();
LOG.info("get readers is "+httpRequest.getReader());
BufferedReader reader = httpRequest.getReader();
String line;
while((line = reader.readLine() != null)){
LOG.info("readline is:"+reader.readLine());
LOG.info("line is:"+line);
buffer.append(line);
}
String requestbody = buffer.toString();
LOG.info("reqparam is "+ requestbody);
JSONObject object = JSONObject.parseObject(requestbody);
return object ;
}