HttpServletRequest 获取请求参数
2021-08-27 本文已影响0人
CNSTT
/**
* 测试获取request参数POST
*
* @param saveSignatureDTO 传参
* @author Tansj
* @since 2021/08/26
*/
@Override
public Map<String, String> testHttpServletRequest2(SaveSignatureDTO saveSignatureDTO, HttpServletRequest request) {
Map<String, String> result = new HashMap<>(16);
log.info("请求方式GET/POST等:============={}", request.getMethod());
log.info("getRequestURL:============={}", request.getRequestURL());
// 获取URL的传参
Enumeration<String> params = request.getParameterNames();
while (params.hasMoreElements()) {
String paraName = params.nextElement();
log.info("paraName:============={}", paraName);
String[] paraValues = request.getParameterValues(paraName);
log.info("paraValues:============={}", paraValues);
result.put(paraName, paraValues.length == 1 ? paraValues[0] : paraValues[paraValues.length - 1]);
}
// 获取body的传参
Map<String, String> map = new HashMap<>(16);
try {
String reader = getAllRequestParam2(request);
log.info("reader:============={}", reader);
map = JSONObject.parseObject(reader, Map.class);
log.info("map:============={}", map);
} catch (IOException e) {
e.printStackTrace();
}
result.putAll(map);
log.info("result:============={}", result);
SortedMap<String, String> sort = new TreeMap<>(result);
log.info("sort:============={}", sort);
return sort;
}
public static Map<String, String> getAllRequestParam(HttpServletRequest request) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
String str = "";
StringBuilder wholeStr = new StringBuilder();
//一行一行的读取body体里面的内容;
while ((str = reader.readLine()) != null) {
wholeStr.append(str);
}
if(wholeStr.toString().startsWith("[")){
Map<String, String> map = new HashMap<>();
map.put("arrayString", wholeStr.toString());
return map;
}else {
//转化成json对象
return JSONObject.parseObject(wholeStr.toString(), Map.class, Feature.OrderedField);
}
}
public static String getAllRequestParam2(HttpServletRequest request) throws IOException {
BufferedReader br = request.getReader();
String str;
StringBuilder wholeStr = new StringBuilder();
while((str = br.readLine()) != null){
wholeStr.append(str);
}
return wholeStr.toString();
}
URL传参
http://localhost:28892/repair/collect/testHttpServletRequest2?sign=B3CB08A44B0B718BD7A7B371E488853D×tamp=1629967927
@RequestBody 传参
{ "billType": "RV", "billNo": "RV2108270001", "billNo1": "RV2108270001", "billNo2": "RV2108270001", "aa": [ { "name": "zs", "age": 15 }, { "name": "ls", "age": 11 } ], "fileAddress": "http://localhost:28892/repair" }
CollectionServiceImpl:653] - 请求方式GET/POST等:=============POST
CollectionServiceImpl:654] - getRequestURL:=============http://localhost:28892/repair/collect/testHttpServletRequest2
CollectionServiceImpl:659] - paraName:=============sign
CollectionServiceImpl:661] - paraValues:=============B3CB08A44B0B718BD7A7B371E488853D
CollectionServiceImpl:659] - paraName:=============timestamp
CollectionServiceImpl:661] - paraValues:=============1629967927
CollectionServiceImpl:669] - reader:============={ "billType": "RV", "billNo": "RV2108270001", "billNo1": "RV2108270001", "billNo2": "RV2108270001", "aa": [ { "name": "zs", "age": 15 }, { "name": "ls", "age": 11 } ], "fileAddress": "http://localhost:28892/repair"}
CollectionServiceImpl:671] - map:============={aa=[{"name":"zs","age":15},{"name":"ls","age":11}], billType=RV, billNo2=RV2108270001, billNo1=RV2108270001, fileAddress=http://localhost:28892/repair, billNo=RV2108270001}
CollectionServiceImpl:676] - result:============={aa=[{"name":"zs","age":15},{"name":"ls","age":11}], billType=RV, sign=B3CB08A44B0B718BD7A7B371E488853D, billNo2=RV2108270001, billNo1=RV2108270001, fileAddress=http://localhost:28892/repair, billNo=RV2108270001, timestamp=1629967927}
CollectionServiceImpl:678] - sort:============={aa=[{"name":"zs","age":15},{"name":"ls","age":11}], billNo=RV2108270001, billNo1=RV2108270001, billNo2=RV2108270001, billType=RV, fileAddress=http://localhost:28892/repair, sign=B3CB08A44B0B718BD7A7B371E488853D, timestamp=1629967927}