代码优化

1.x
tanghc 5 years ago
parent c274b6508f
commit da172a9b39
  1. 6
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java
  2. 30
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java
  3. 116
      sop-gateway/src/main/java/com/gitee/sop/gateway/config/PreParamFilter.java

@ -67,12 +67,8 @@ public class OpenUtil {
for (Map.Entry<String, String[]> entry : entrySet) {
String name = entry.getKey();
String[] values = entry.getValue();
if (values.length == 1) {
if (values.length >= 1) {
retMap.put(name, values[0]);
} else if (values.length > 1) {
retMap.put(name, values);
} else {
retMap.put(name, "");
}
}

@ -127,13 +127,26 @@ public class AlipayController {
// http://localhost:2222/alipay.story.get/
@ApiMapping(value = "alipay.story.get")
public Story getStory() {
public Story getStory(Story param) {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(alipay.story.get1.0)");
story.setName("海底小纵队(alipay.story.get1.0), param:" + param);
return story;
}
/**
*
* @param param 对应biz_content中的内容并自动JSR-303校验
* @return
*/
@ApiMapping(value = "alipay.story.get", version = "1.2")
public Story getStory11(Story param) {
Story story2 = new Story();
story2.setId(1);
story2.setName("海底小纵队(alipay.story.get1.2), param:" + param);
return story2;
}
/**
* 参数绑定
* @param story 对应biz_content中的内容并自动JSR-303校验
@ -153,19 +166,6 @@ public class AlipayController {
return storyVO;
}
/**
*
* @param story 对应biz_content中的内容并自动JSR-303校验
* @return
*/
@ApiMapping(value = "alipay.story.get", version = "1.2")
public Story getStory11(Story story) {
Story story2 = new Story();
story2.setId(1);
story2.setName("海底小纵队(alipay.story.get1.2)");
return story2;
}
/**
* 演示文档表格树
* @param story

@ -0,0 +1,116 @@
package com.gitee.sop.gateway.config;
import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.param.ParamNames;
import com.gitee.sop.gatewaycommon.zuul.ZuulContext;
import com.gitee.sop.gatewaycommon.zuul.filter.BaseZuulFilter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.netflix.zuul.http.HttpServletRequestWrapper;
import com.netflix.zuul.http.ServletInputStreamWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpMethod;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author tanghc
*/
public class PreParamFilter extends BaseZuulFilter {
@Override
protected FilterType getFilterType() {
return FilterType.PRE;
}
@Override
protected int getFilterOrder() {
return 0;
}
@Override
protected Object doRun(RequestContext requestContext) throws ZuulException {
ApiParam apiParam = ZuulContext.getApiParam();
JSONObject bizContent = apiParam.getJSONObject(ParamNames.BIZ_CONTENT_NAME);
// 修改biz_content中的参数
bizContent.put("name", "修改了111");
apiParam.put(ParamNames.BIZ_CONTENT_NAME, bizContent.toJSONString());
HttpServletRequest request = requestContext.getRequest();
String contentType = request.getContentType();
if (StringUtils.containsIgnoreCase(contentType, "json")) {
byte[] bytes = apiParam.toJSONString().getBytes(StandardCharsets.UTF_8);
requestContext.setRequest(new HttpServletRequestWrapper(requestContext.getRequest()) {
@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStreamWrapper(bytes);
}
@Override
public byte[] getContentData() {
return bytes;
}
@Override
public int getContentLength() {
return bytes.length;
}
@Override
public long getContentLengthLong() {
return bytes.length;
}
});
} else if(StringUtils.containsIgnoreCase(contentType, "form")) {
List<String> list = Lists.newArrayList();
try {
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
list.add(entry.getKey() + "=" + URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String paramsStr = StringUtils.join(list, "&");
byte[] bytes = paramsStr.getBytes(StandardCharsets.UTF_8);
requestContext.setRequest(new HttpServletRequestWrapper(requestContext.getRequest()) {
@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStreamWrapper(bytes);
}
@Override
public byte[] getContentData() {
return bytes;
}
@Override
public int getContentLength() {
return bytes.length;
}
@Override
public long getContentLengthLong() {
return bytes.length;
}
});
} else if(HttpMethod.GET.name().equalsIgnoreCase(request.getMethod())) {
Map<String, List<String>> newParams = Maps.newHashMap();
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
newParams.put(entry.getKey(), Collections.singletonList(String.valueOf(entry.getValue())));
}
requestContext.setRequestQueryParams(newParams);
}
return null;
}
}
Loading…
Cancel
Save