【场景】
服务端点对点通知。
A服务发起请求B服务,B同步返回接收成功;然后B开始处理逻辑;B处理完成后异步通知给A;A接收请求并处理,同步回写响应给B;完成。
【先上代码】
服务端(接收端)代码:
客户端(请求端)HttpUtil工具类:
客户端测试方法:
【测试结论】
Tomcat和jetty对于HttpServletRequest.getParameterMap()的处理不同。jetty可以直接获取到请求参数;而Tomcat获取不到。下面是Tomcat的日志:
六月 11, 2019 10:11:53 上午 org.apache.tomcat.util.http.Parameters processParameters 信息: Character decoding failed. Parameter [{"interfaceName":"RegisterNotify","merSignMsg":"APrWHHydX41atXjfadKBfDPUhKBQbZ6fTKcnHGtVBhe9qpSfArVqRFrlf2wgw9gzmMnGo3x15XKXAZnC51WU60FXNVj2kaxpWYzpuh6rvUDrDVQV6Z7SHEI8GvrMLE8uOG2TPR0Xu6v71o8u8TJsWsiVOP/ncsAHSzSz%%2B2Ch7N3E5ePCQi84To7LvSO5HrtUUmTbc%2BrmG2frJfYJNfvsxuGvt9U2MqmFeWFE98fK5e5SFUSSZLtqj42N18ppSZWSxN3MleGDTsy75zR3JxO6ol99lCPea4zqLmnUoEFlnJ3J6vXXUVXnMuSX5Mw%3D%3D","merchantId":"M100002734","tranData":"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iR0JLIiA%2FPjxCMkNSZXM%2BPHN0YXR1cz4wPC9zdGF0dXM%2BPGJpbmRTdHM%2BMTwvYmluZFN0cz48Y3VzdG9tZXJJZD5DMTAwMDE0Mjc1PC9jdXN0b21lcklkPjxjb21wYW55PtDs1t3TzrHpzOzPwsLD087Qxc%2Bi18nRr9PQz965q8u%2BPC9jb21wYW55PjxyZWZ1c2VSZWFzb24vPjwvQjJDUmVzPg%3D%3D","version":"B2C1.0"}] with value [] has been ignored. Note that the name and value quoted here may be corrupted due to the failed decoding. Use debug level logging to see the original, non-corrupted values. Note: further occurrences of Parameter errors will be logged at DEBUG level. map count:0
Character decoding failed.Parameter [dd] with value [] has been ignored. 百度翻译为:字符解码失败。已忽略值为[]的参数[dd]。
我的Tomcat版本是7.0.93;jetty版本是6.1.26。通过比较两者的servlet-api.jar,发现Tomcat7的servlet-api的版本是3.0,而jetty的servlet-api的版本是2.5。或许是这种版本的差异导致结果不同。试图跟踪源码来一识庐山真面目,jetty的进去了,但从RequestWapper类里也看不到什么;Tomcat的则进不去。
进一步通过Tomcat测试发现:请求参数里的merSignMsg,当我改变其值(去掉开头的一些字符)时,Tomcat就能获取到了。看来还是Tomcat处理字符编码的问题。
【话说回来】
话说回来,上面案例content-type用form实在不合适,因为想获取到请求数据,得取key的值而不是value,这不符合常用的套路啊,由此我的同事那天在接收数据时很费了一番折腾。像这种上送json字符串的,改用text/plain更合适。这样的话,接收端通过读取HttpServletRequest的输入流就可以获取到请求数据。
附上读取HttpServletRequest流的代码:
package com; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class ReadHttpRequest { /** * 从request.ParameterMap获取数据---适用于表单请求 * @param request * @return * @throws UnsupportedEncodingException * @throws Exception */ public static Map getRequestMap(HttpServletRequest request) throws UnsupportedEncodingException,Exception { Map result = new HashMap(); Map<String, String[]> map = request.getParameterMap(); if (0 == map.size()) { System.out.println("未获取到任何请求参数."); // throw new Exception("未获取到任何请求参数."); return result; } for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) { Map.Entry element = (Map.Entry) iter.next(); Object strKey = element.getKey(); //key值 String[] value = (String[]) element.getValue(); //value,数组形式 String values = ""; for (int i = 0; i < value.length; i++) { values += "," + value[i]; } result.put(strKey.toString(), values.replaceFirst(",", "")); } return result; } /** * 从输入流获取数据---适用于流请求 * @param request * @return */ public static String getRequestInputStream(HttpServletRequest request) { InputStream in = null; ByteArrayOutputStream out = null; try { in = request.getInputStream(); out = new ByteArrayOutputStream(); in2OutStream(in, out, 1024 * 1024); return out.toString("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } try { if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } private static void in2OutStream(InputStream in, OutputStream out, int bufferSize) throws IOException { byte[] buffer = new byte[bufferSize];// 缓冲区 for (int bytesRead = 0; (bytesRead = in.read(buffer)) != -1; ) { out.write(buffer, 0, bytesRead); Arrays.fill(buffer, (byte) 0); } } }
原文地址:https://www.cnblogs.com/buguge/p/11002836.html