package com.github.sd4324530.fastweixin.servlet;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 微信公众平台交互操作基类，提供几乎所有微信公众平台交互方式
 * 基于springmvc框架，方便使用此框架的项目集成
 *
 * @author peiyu
 */
@Controller
public abstract class WeixinControllerSupport extends WeixinSupport {

    /**
     * 绑定微信服务器
     *
     * @param request 请求
     * @return 响应内容
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    protected final String bind(HttpServletRequest request) {
        if (isLegal(request)) {
            //绑定微信服务器成功
            return request.getParameter("echostr");
        } else {
            //绑定微信服务器失败
            return "";
        }
    }

    /**
     * 微信消息交互处理
     *
     * @param request http 请求对象
     * @return 响应给微信服务器的消息报文
     * @throws ServletException 异常
     * @throws IOException      IO异常
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    protected final String process(HttpServletRequest request) throws ServletException, IOException {
        if (!isLegal(request)) {
            return "";
        }
        return processRequest(request);
    }
}
@@ -365,16 +365,63 @@
 equest;%0A
+import javax.servlet.http.HttpServletResponse;%0A
 import j
@@ -1078,16 +1078,39 @@
 tp %E8%AF%B7%E6%B1%82%E5%AF%B9%E8%B1%A1%0A
+     * @param response%0A
      * @
@@ -1130,16 +1130,16 @@
 %E5%8A%A1%E5%99%A8%E7%9A%84%E6%B6%88%E6%81%AF%E6%8A%A5%E6%96%87%0A
-
      * @
@@ -1259,34 +1259,16 @@
 d.POST)%0A
-    @ResponseBody%0A
     prot
@@ -1279,22 +1279,20 @@
 d final 
-String
+void
  process
@@ -1318,16 +1318,46 @@
  request
+, HttpServletResponse response
 ) throws
@@ -1405,9 +1405,8 @@
 if (
-!
 isLe
@@ -1433,35 +1433,66 @@
         
-return %22%22
+String result = processRequest(request)
 ;%0A
+%0A
         
 %7D%0A      
@@ -1487,49 +1487,161 @@
     
-%7D%0A        return processRequest(request);
+    //%E8%AE%BE%E7%BD%AE%E6%AD%A3%E7%A1%AE%E7%9A%84 content-type %E4%BB%A5%E9%98%B2%E6%AD%A2%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%0A            response.setContentType(%22text/xml;charset=UTF-8%22);%0A            response.getWriter().write(result);%0A        %7D
 %0A   

解决 @ResponseBody 导致的中文乱码问题