introduce
Everyone knows that the callback link requirements of WeChat Pay cannot be followed by parameters, but they also need to receive the returned xml data. I started using @RequestBody annotation on the parameters, hoping to get the xml data, the test failed. Finally, using HttpServletRequest to obtain the data successfully.
Sample code
@RequestMapping("/weixinpay/callback")public String callBack(HttpServletRequest request){ InputStream is = request.getInputStream(); String xml = StreamUtil.inputStream2String(is, "UTF-8") /** * Later, convert xml into Map to process logically based on the data*/} /** * InputStream stream is converted into String string* @param inStream InputStream stream* @param encoding Encoding format* @return String string*/public static String inputStream2String(InputStream inStream, String encoding){ String result = null; try { if(inStream != null){ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] tempBytes = new byte[_buffer_size]; int count = -1; while((count = inStream.read(tempBytes, 0, _buffer_size)) != -1){ outStream.write(tempBytes, 0, count); } tempBytes = null; outStream.flush(); result = new String(outStream.toByteArray(), encoding); } } catch (Exception e) { result = null; } return result;}Summarize
The above is the entire content of this article. I hope it will help you study or work. If you have any questions, you can leave a message to communicate.