湘水渔叟 發表於 2019-7-25 11:39:00

spring boot 开发微信公众号

<p>因为公司需要开发微信公众号模块,所以在网上看了很多关于spring boot微信公众号的开发,都感觉不能满足自己对代码简单处理。</p>
<p>按照我的思路,完成了一个微信公众号的业务功能的开发,并总结如下:</p>
<p>1,创建用于接收和返回的对象</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lombok.Data;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.annotation.XmlAccessType;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.annotation.XmlAccessorType;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.annotation.XmlRootElement;

@Data
@XmlRootElement(name</span>="xml"<span style="color: rgba(0, 0, 0, 1)">)
@XmlAccessorType(XmlAccessType.FIELD)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> WeChatMessageBo {
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String ToUserName;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String FromUserName;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> CreateTime;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String MsgType;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String Event;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 消息id</span>
    <span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Long MsgId;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文本内容</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String Content;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 图片链接(由系统生成)</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String PicUrl;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 图片消息媒体id,可以调用多媒体文件下载接口拉取数据</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String MediaId;
  
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String EventKey;
}</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.annotation.XmlAccessType;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.annotation.XmlAccessorType;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.annotation.XmlElementWrapper;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.annotation.XmlRootElement;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;

@Data
@XmlRootElement(name</span>="xml"<span style="color: rgba(0, 0, 0, 1)">)
@XmlAccessorType(XmlAccessType.FIELD)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> WeChatMessageVo {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送方的账号</span>
    <span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> String FromUserName;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收方的账号(OpenID)</span>
    <span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> String ToUserName;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 消息创建时间</span>
    <span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Long CreateTime;
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 消息类型
   * text 文本消息
   * image 图片消息
   * voice 语音消息
   * video 视频消息
   * music 音乐消息
   * news 图文消息
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> String MsgType;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 语音</span>
    @XmlElementWrapper(name="Voice"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String[] MediaId ;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文本内容</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String Content;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> ArticleCount;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">图文消息</span>
    @XmlElementWrapper(name="Articles"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> List&lt;NewItems&gt;<span style="color: rgba(0, 0, 0, 1)"> item;

}</span></pre>
</div>
<p>  笔者把请求对象全部塞到bo里面,把返回的数据全部塞到vo里面,把数据简单处理,不再根据请求类型拆分不同的vo和bo</p>
<p>&nbsp;&nbsp;&nbsp; 2创建controller,直接采用jdk中原装的 unmarshaller 和 marshaller 对事件的报文进行解析和封装</p>
<p>&nbsp;</p>
<pre><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(102, 14, 122, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1)"><span style="color: rgba(0, 0, 128, 1)">&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> io.swagger.annotations.Api;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> io.swagger.annotations.ApiOperation;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lombok.extern.slf4j.Slf4j;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.beans.factory.annotation.Autowired;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.http.MediaType;
</span><span style="color: rgba(0, 0, 255, 1)">import</span> org.springframework.web.bind.annotation.*<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.annotation.PostConstruct;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServletRequest;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServletResponse;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.JAXBContext;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.JAXBException;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.Marshaller;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.xml.bind.Unmarshaller;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;


@RestController
@Api(tags </span>= "微信公众号服务"<span style="color: rgba(0, 0, 0, 1)">)
@RequestMapping(value </span>= "/weChart"<span style="color: rgba(0, 0, 0, 1)">)
@Slf4j
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> WeChartController {

    @Autowired(required </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> WeChartService weChartService;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Unmarshaller unmarshaller;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Marshaller marshaller;

    @PostConstruct
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> init(){
      JAXBContext context </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            context </span>= JAXBContext.newInstance(WeChatMessageBo.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            unmarshaller </span>=<span style="color: rgba(0, 0, 0, 1)"> context.createUnmarshaller();
      } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (JAXBException e) {
         log.info(e.toString());
      }
      </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            context </span>= JAXBContext.newInstance(WeChatMessageVo.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);

            marshaller </span>=<span style="color: rgba(0, 0, 0, 1)"> context.createMarshaller();

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_ENCODING,</span>"UTF-8"<span style="color: rgba(0, 0, 0, 1)">);

      } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (JAXBException e) {
            log.info(e.toString());
      }


    }


    @ApiOperation(value </span>= "验证"<span style="color: rgba(0, 0, 0, 1)">)
    @RequestMapping(value </span>= "auth",method =<span style="color: rgba(0, 0, 0, 1)"> RequestMethod.GET)
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> String auth(@RequestParam(name="signature"<span style="color: rgba(0, 0, 0, 1)">) String signature,
                                          @RequestParam(name</span>="timestamp"<span style="color: rgba(0, 0, 0, 1)">) String timestamp,
                                          @RequestParam(name</span>="nonce"<span style="color: rgba(0, 0, 0, 1)">) String nonce,
                                          @RequestParam(name</span>="echostr"<span style="color: rgba(0, 0, 0, 1)">) String echostr){
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">weChartService.auth(signature,timestamp,nonce,echostr);

    }

    @ApiOperation(value </span>= "接收事件"<span style="color: rgba(0, 0, 0, 1)">)
    @RequestMapping(value </span>= "auth",method = RequestMethod.POST, produces =<span style="color: rgba(0, 0, 0, 1)"> MediaType.APPLICATION_XML_VALUE)
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> event(HttpServletRequest request,HttpServletResponse response){
      </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            WeChatMessageBo weChatMessageBo </span>=<span style="color: rgba(0, 0, 0, 1)"> (WeChatMessageBo) unmarshaller.unmarshal(request.getInputStream());
            Object res </span>=<span style="color: rgba(0, 0, 0, 1)"> weChartService.response(weChatMessageBo);

            response.setCharacterEncoding(</span>"UTF-8"<span style="color: rgba(0, 0, 0, 1)">);

            marshaller.marshal(res, response.getWriter());

      } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
            e.printStackTrace();
      }

    }

}</span></pre>
</div>
<pre></pre>
<pre>3,建一个注解和一个对应接口,用来区分不同的请求类型,当然,其他使用if else也是可以实现的。这里为了代码更加简练<br>注解使用@Component,之后把引用该注解的类,就不需要再次添加该注解<span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(102, 14, 122, 1); font-weight: bold; font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.stereotype.Component;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.annotation.ElementType;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.annotation.Retention;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.annotation.RetentionPolicy;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.lang.annotation.Target;

@Component
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
</span><span style="color: rgba(0, 0, 255, 1)">public</span> @<span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> Hkey {
      String name() </span><span style="color: rgba(0, 0, 255, 1)">default</span> ""<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>&nbsp;</p>
<pre>创建一个接口,用来处理微信调用的不同类型<span style="color: rgba(0, 0, 128, 1); font-weight: bold"><br></span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> Hander {
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object Hander(WeChatMessageBo bo);
}</span></pre>
</div>
<pre></pre>
<pre>4:service 层逻辑处理<span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(102, 14, 122, 1); font-weight: bold; font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> WeChartService <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> ApplicationContextAware {
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
   * 自定义token, 用作生成签名,从而验证安全性
   * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    @Value(</span>"${weChart.token}"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String token ;

    @Value(</span>"${weChart.appid}"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)">   String appid;

    @Value(</span>"${weChart.secret}"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)">   String secret;

    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> WeChartApi weChartApi;

    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> CacheAsyncService cacheAsyncService;

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String auth(String signature, String timestamp, String nonce, String echostr) {

      String sortStr </span>=<span style="color: rgba(0, 0, 0, 1)"> sort(token,timestamp,nonce);
      String mySignature </span>=<span style="color: rgba(0, 0, 0, 1)"> shal(sortStr);

      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!"".equals(signature) &amp;&amp; !"".equals(mySignature) &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> signature.equals(mySignature)){
          </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> echostr;
      }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
      }

    }


   
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String sort(String token, String timestamp, String nonce) {
      String[] strArray </span>=<span style="color: rgba(0, 0, 0, 1)"> {token, timestamp, nonce};
      Arrays.sort(strArray);
      StringBuilder sb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder();
      </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String str : strArray) {
            sb.append(str);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sb.toString();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String shal(String str){
      </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            MessageDigest digest </span>= MessageDigest.getInstance("SHA-1"<span style="color: rgba(0, 0, 0, 1)">);
            digest.update(str.getBytes());
            </span><span style="color: rgba(0, 0, 255, 1)">byte</span> messageDigest[] =<span style="color: rgba(0, 0, 0, 1)"> digest.digest();

            StringBuffer hexString </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuffer();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 字节数组转换为 十六进制 数</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; messageDigest.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                String shaHex </span>= Integer.toHexString(messageDigest &amp; 0xFF<span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (shaHex.length() &lt; 2<span style="color: rgba(0, 0, 0, 1)">) {
                  hexString.append(</span>0<span style="color: rgba(0, 0, 0, 1)">);
                }
                hexString.append(shaHex);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> hexString.toString();

      } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (NoSuchAlgorithmException e) {
            e.printStackTrace();
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> ""<span style="color: rgba(0, 0, 0, 1)">;
    }


</span><span style="color: rgba(0, 0, 255, 1)">private</span> HashMap&lt;String, Hander&gt; handers=<span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();


</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object response(WeChatMessageBo msg) {
    String msgType </span>=<span style="color: rgba(0, 0, 0, 1)"> msg.getMsgType();
    String Event </span>=<span style="color: rgba(0, 0, 0, 1)"> msg.getEvent();
    String EventKey </span>=<span style="color: rgba(0, 0, 0, 1)"> msg.getEventKey();
    Hander hander </span>= handers.get(msgType + "." + Event + "." +<span style="color: rgba(0, 0, 0, 1)"> EventKey);
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (hander!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">hander.Hander(msg);
    }
   </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}

@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setApplicationContext(ApplicationContext applicationContext) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> BeansException {
    Map</span>&lt;String, Hander&gt; beansOfType = applicationContext.getBeansOfType(Hander.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> ( Hander hander:beansOfType.values()) {
      Hkey hkey </span>= hander.getClass().getAnnotation(Hkey.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
      handers.put(hkey.name(),hander);
    }

}

}</span></pre>
</div>
<pre></pre>
<pre>该步使用利用了spring的ApplicationContext,对之后使用的hander 进行处理,方便responer方法的使用(该service 为节选,功能都存在,拷贝时可能代码可能出现问题)<span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(102, 14, 122, 1); font-weight: bold; font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><br><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>5具体方法的实现:<br>用户回复,回复用户图文消息或者文子消息<span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(102, 14, 122, 1); font-weight: bold; font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<div class="cnblogs_code">
<pre>@Hkey(name="text.null.null"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Replay <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Hander {

    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Mapper mapper;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> String unfind= "对不起,没找到你搜索的数据。\n要不,联系一下客服?或者我给你笑一个?\uE057"<span style="color: rgba(0, 0, 0, 1)">;

    @Override
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">回复用户和图文消息</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object Hander(WeChatMessageBo msg) {
      WeChatMessageVo out </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WeChatMessageVo();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把原来的发送方设置为接收方</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setToUserName(msg.getFromUserName());
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把原来的接收方设置为发送方</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setFromUserName(msg.getToUserName());
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取接收的消息类型</span>
      String msgType =<span style="color: rgba(0, 0, 0, 1)"> msg.getMsgType();
      String content </span>=<span style="color: rgba(0, 0, 0, 1)"> msg.getContent();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置消息创建时间</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setCreateTime(System.currentTimeMillis());
      out.setMsgType(</span>"news"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用户自定义数据</span>
      List&lt;Data&gt; list =<span style="color: rgba(0, 0, 0, 1)"> mapper.selectByNameLike(content);

      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(CollectionUtils.isEmpty(gameList)){
            out.setMsgType(</span>"text"<span style="color: rgba(0, 0, 0, 1)">);
            out.setContent(unfind);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out;
      }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{

            ArrayList</span>&lt;NewItems&gt; items = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;NewItems&gt;<span style="color: rgba(0, 0, 0, 1)">(list.size());
            list.stream().forEach(item</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">{
                NewItems newItems </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NewItems();
                newItems.setTitle(item.getName());
                newItems.setUrl(item.getUrl());
                newItems.setDescription(item.getDescription());
                newItems.setPicUrl(item.getBackgroundPicture());
                items.add(newItems);
            });
            out.setArticleCount(gameList.size());
            out.setItem(items);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out;
      }
    }
}</span></pre>
</div>
<p>&nbsp;</p>
<pre><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="font-style: italic"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="font-style: italic"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>菜单按钮,作者菜单中有个按钮V31_HELP,其他使用可能会有很多按钮,就直接修改注解名字即可<span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="font-style: italic"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="font-style: italic"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(102, 14, 122, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<div class="cnblogs_code">
<pre>@Hkey(name="event.CLICK.V31_HELP"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> ForHelp <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Hander {
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> Stringvalue= "你好,请拨打电话\n"<span style="color: rgba(0, 0, 0, 1)">;

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">联系客服</span>
<span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object Hander(WeChatMessageBo msg) {
      WeChatMessageVo out </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WeChatMessageVo();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把原来的发送方设置为接收方</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setToUserName(msg.getFromUserName());
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把原来的接收方设置为发送方</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setFromUserName(msg.getToUserName());
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取接收的消息类型</span>
      String msgType =<span style="color: rgba(0, 0, 0, 1)"> msg.getMsgType();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置消息创建时间</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setCreateTime(System.currentTimeMillis());
      out.setMsgType(</span>"text"<span style="color: rgba(0, 0, 0, 1)">);
      out.setContent(value);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out;
    }
}</span></pre>
</div>
<pre></pre>
<p>&nbsp;</p>
<pre></pre>
<pre>用户订阅时代码,此处代码,笔者未做测试,使用的用户要看一下注解名字是否正确<span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(102, 14, 122, 1); font-weight: bold; font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<div class="cnblogs_code">
<pre>@Hkey(name = "event.subscribe.null"<span style="color: rgba(0, 0, 0, 1)">)
@Service
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Subscribe <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Hander {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用户订阅</span>
<span style="color: rgba(0, 0, 0, 1)">
    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Mapper mapper;
    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object Hander(WeChatMessageBo msg) {
      WeChatMessageVo out </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WeChatMessageVo();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把原来的发送方设置为接收方</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setToUserName(msg.getFromUserName());
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把原来的接收方设置为发送方</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setFromUserName(msg.getToUserName());
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取接收的消息类型
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">String msgType = msg.getMsgType();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置消息创建时间</span>
<span style="color: rgba(0, 0, 0, 1)">      out.setCreateTime(System.currentTimeMillis());
         
      out.setMsgType(</span>"news"<span style="color: rgba(0, 0, 0, 1)">);
      out.setArticleCount(</span>4<span style="color: rgba(0, 0, 0, 1)">);
      ArrayList</span>&lt;NewItems&gt; items = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;NewItems&gt;(4<span style="color: rgba(0, 0, 0, 1)">);
         

       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用户自定义数据</span>
      List&lt;Data&gt; list =<span style="color: rgba(0, 0, 0, 1)"> mapper.selectSubScribeData();
      list .stream().forEach(item</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">{
            NewItems newItems </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NewItems();
            newItems.setTitle(item.getTitle());
            newItems.setUrl(item.getUrl());
            newItems.setDescription(item.getDes());
            newItems.setPicUrl(item.getPicUrl());
            items.add(newItems);
      });
      out.setItem(items);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out;
    }
}</span></pre>
</div>
<p>&nbsp;查询数据库的就不再写了,大概工程结构就是这个样子的,我不想写一堆的xml解析,也不想拷贝的或者是引入各种包,既然如此,那就直接原生的marshaller 吧,用的时候,感觉也很不错。就分享给大家吧<span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(0, 128, 0, 1); font-weight: bold"><span style="color: rgba(128, 128, 0, 1)"><span style="color: rgba(102, 14, 122, 1); font-weight: bold; font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(128, 128, 128, 1); font-style: italic"><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><span style="color: rgba(0, 0, 128, 1); font-weight: bold"><span><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p><br><br>
来源:https://www.cnblogs.com/see-saw/p/11243206.html
頁: [1]
查看完整版本: spring boot 开发微信公众号