SpringBoot+Zuul过滤器中获取每个请求的参数
package com.ydy.zuul.util;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.google.common.collect.Maps;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.http.HttpServletRequestWrapper;
import com.ydy.common.constant.Constant;
import com.ydy.common.util.JsonUtil;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.RequestFacade;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StreamUtils;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class ZuulParameterUtil {
private static Logger LOGGER = LoggerFactory.getLogger(ZuulParameterUtil.class);
/**
* 获取zuul拦击请求参数
* @param ctx
*/
public static Map<String, Object> getRequestParams(RequestContext ctx) {
String method = ctx.getRequest().getMethod();
//判断是POST请求还是GET请求(不同请求获取参数方式不同)
LinkedHashMap<String,Object> param = new LinkedHashMap<>();
try {
if ("GET".equals(method.toUpperCase())) {
Map<String,String[]> map = ctx.getRequest().getParameterMap();
if (!(Objects.isNull(map) || map.isEmpty())) {
param = Maps.newLinkedHashMap();
for (Map.Entry<String, String[]> entry : map.entrySet()) {
param.put(entry.getKey(), entry.getValue()[0]);
}
}
} else if ("POST".equals(method.toUpperCase())) {
String paramsType = ctx.getRequest().getHeader(Constant.ACCEPT);
if(paramsType.equals(Constant.APPLICATION_JSON)){
InputStream inputStream = ctx.getRequest().getInputStream();
String body = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));
param = JsonUtil.jsonToObject(body, new TypeReference<LinkedHashMap<String, Object>>() {});
}else{
StandardMultipartHttpServletRequest mul = new StandardMultipartHttpServletRequest(ctx.getRequest());
HttpServletRequestWrapper sere = (HttpServletRequestWrapper)mul.getRequest();
StandardMultipartHttpServletRequest fill = (StandardMultipartHttpServletRequest) sere.getRequest();;
RequestFacade facsde = (RequestFacade)fill.getRequest();
Map<String,String[]> parameterMap = facsde.getParameterMap();
for(String key : parameterMap.keySet()){
param.put(key,parameterMap.get(key)[0]);
}
}
}
} catch (Exception e) {
LOGGER.error("***************ZuulParameterUtil->getRequestParams throw Exception:{}***************", e);
}
return param;
}
}
package com.ydy.common.constant;
/**
* 常量类
*/
public interface Constant {
final String AUTHORIZATION ="Authorization";
final String ACCEPT = "Accept";
final String HOST = "Host";
final String USER_AGENT = "User-Agent";
final String APPLICATION_JSON = "application/json";
}
package com.ydy.common.util;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException;
import java.util.Map;
public class JsonUtil {
private final static ObjectMapper objectMapper = new ObjectMapper();
private static String objectToJson(Object object) {
ObjectMapper om = new ObjectMapper();
String json = "";
try {
try {
json = om.writeValueAsString(object);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
public static String getJson(Object obj){
return objectToJson(obj);
}
public static <T> T jsonToObject(String json, TypeReference<T> typeReference) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, typeReference);
} catch (JsonParseException e) {
} catch (JsonMappingException e) {
} catch (IOException e) {
}
return null;
}
public static Map<String,Object> formatDataToMap(String data) throws JsonParseException, JsonMappingException, IOException{
Map<String,Object> maps = objectMapper.readValue(data, Map.class);
return maps;
}
}