Skip to content

Commit f9e7f18

Browse files
authored
zuul中新增参数modify
zuul中新增参数modify
1 parent 79cc672 commit f9e7f18

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

ch8-7/ch8-7-zuul-server/src/main/java/cn/springcloud/book/filter/ModifyRequestEntityFilter.java

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,16 @@ public boolean shouldFilter() {
3838

3939
@Override
4040
public Object run() throws ZuulException {
41-
try {
42-
RequestContext context = RequestContext.getCurrentContext();
43-
String charset = context.getRequest().getCharacterEncoding();
44-
InputStream in = (InputStream) context.get("requestEntity");
45-
if (in == null)
46-
in = context.getRequest().getInputStream();
47-
String body = StreamUtils.copyToString(in, Charset.forName(charset));
48-
// 这里是新增的参数
49-
body += "&weight=140";
50-
byte[] bytes = body.getBytes(charset);
51-
context.setRequest(new HttpServletRequestWrapper(context.getRequest()) {
52-
53-
@Override
54-
public ServletInputStream getInputStream() throws IOException {
55-
return new ServletInputStreamWrapper(bytes);
56-
}
57-
58-
@Override
59-
public int getContentLength() {
60-
return bytes.length;
61-
}
62-
63-
@Override
64-
public long getContentLengthLong() {
65-
return bytes.length;
66-
}
67-
});
68-
} catch (IOException e) {
69-
ReflectionUtils.rethrowRuntimeException(e);
70-
}
41+
RequestContext ctx = RequestContext.getCurrentContext();
42+
HttpServletRequest request = ctx.getRequest();
43+
request.getParameterMap();
44+
Map<String, List<String>> requestQueryParams = ctx.getRequestQueryParams();
45+
if (requestQueryParams == null) requestQueryParams = new HashMap<>();
46+
//这里添加新增参数的value,注意,只取list的0位
47+
ArrayList<String> arrayList = new ArrayList<>();
48+
arrayList.add("1wwww");
49+
requestQueryParams.put("test", arrayList);
50+
ctx.setRequestQueryParams(requestQueryParams);
7151
return null;
7252
}
7353
}

0 commit comments

Comments
 (0)