Skip to content

Commit

Permalink
调整框架为springmvc,支持jetty嵌入式开发
Browse files Browse the repository at this point in the history
  • Loading branch information
戴小明 committed Jan 8, 2017
1 parent 65df19f commit 6bf33e3
Show file tree
Hide file tree
Showing 21 changed files with 414 additions and 833 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# 忽略掉所有后缀名为.iml的文件.
*.iml
# 忽略掉IDEA产生的文件.
.idea/
.idea/
logs/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
1. 使用之前需要有微信公众号的帐号以及图灵机器人的帐号,没有的请戳[微信公众号申请](https://mp.weixin.qq.com/cgi-bin/readtemplate?t=register/step1_tmpl&lang=zh_CN)[图灵机器人帐号注册](http://tuling123.com/register/email.jhtml),。
2. 在src/main/resources目录下的config.properties文件里面配置相关的key。
3. 发布到SAE的话需要把项目打包成war文件,然后删除xml-apis-1.0.b2.jar文件,这个包与SAE运行环境冲突。
4. 微信公众号URL配置为`http://robot.mandydai.com/weChat/receiveMessage.action`,其中`robot.mandydai.com`是你自己的域名,token与`config.properties`文件配置一致即可。
4. 微信公众号URL配置为`http://robot.mandydai.com/weChat/receiveMessage.action`,其中`robot.mandydai.com`是你自己的域名,token与`app.properties`文件配置一致即可。
110 changes: 50 additions & 60 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,66 @@
<packaging>war</packaging>
<version>1.0</version>
<name>weChatRobot</name>
<url>http://robot.mandydai.com</url>
<url>http://robot.doodl6.com</url>

<!-- 属性配置 -->
<properties>
<jdk.version>1.7</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<struts.version>2.3.16</struts.version>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
<project.charset>UTF-8</project.charset>
<junit.version>4.12</junit.version>
<spring.version>3.2.4.RELEASE</spring.version>
<jetty.version>7.6.21.v20160908</jetty.version>
<slf4j.version>1.6.5</slf4j.version>
</properties>

<repositories>
<repository>
<id>opensource</id>
<name>taocode nexus</name>
<url>https://repo1.maven.org/maven2/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<!-- jetty支持servlet依赖包 -->
<!-- 单元测试依赖包 -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5</artifactId>
<version>6.1.14</version>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- jetty支持jsp依赖包 -->
<!-- jetty依赖包 -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1-6.0.1</version>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<!-- struts2基本依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<!-- struts2支持注解action依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- struts2支持spring的依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- spring基本依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- spring持久化依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<!-- spring 支持servlet的依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<version>${spring.version}</version>
</dependency>
<!-- fastjson依赖包(用于json转换) -->
<dependency>
Expand All @@ -91,7 +85,23 @@
<artifactId>xstream</artifactId>
<version>1.4.8</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>slf4j-api</artifactId>-->
<!--<version>${slf4j.version}</version>-->
<!--</dependency>-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>

<build>
<finalName>weChatRobot</finalName>
<plugins>
Expand All @@ -103,27 +113,6 @@
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.2.v20120308</version>
<configuration>
<stopKey>stop</stopKey>
<stopPort>5599</stopPort>
<webAppConfig>
<contextPath>/</contextPath>
<defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
</webAppConfig>
<!-- 设置热部署间隔 -->
<scanIntervalSeconds>10</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
Expand All @@ -134,4 +123,5 @@
</plugin>
</plugins>
</build>

</project>
25 changes: 25 additions & 0 deletions src/main/java/com/mandy/wechatrobot/constant/AppConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mandy.wechatrobot.constant;

/**
* 应用常量类
* Created by martindai on 2017/1/8.
*/
public final class AppConstants {

/**
* 应用id
*/
public static String APP_ID = null;
/**
* 应用秘钥
*/
public static String APP_SECRET = null;
/**
* 令牌
*/
public static String TOKEN = null;
/**
* 图灵机器人应用key
*/
public static String API_KEY = null;
}
117 changes: 9 additions & 108 deletions src/main/java/com/mandy/wechatrobot/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
import com.mandy.wechatrobot.util.MessageUtil;
import com.mandy.wechatrobot.util.WeChatUtil;
import com.mandy.wechatrobot.vo.TextMessage;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -21,60 +16,22 @@
import java.util.Map;

@Controller
@Scope("prototype")
@Namespace("/weChat")
public class MainController extends ActionSupport {
@RequestMapping("/weChat")
public class MainController {

private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory
.getLogger(MainController.class);
private static Logger logger = Logger.getLogger(MainController.class);

@Resource(name = "eventMessageHandle")
@Resource
private WeChatMessageHandle eventMessageHandle;

@Resource(name = "textMessageHandle")
@Resource
private WeChatMessageHandle textMessageHandle;

/**
* 微信的appid
*/
private static String appid;
/**
* 微信的secret
*/
private String secret;
/**
* 关注公众号的微信用户的唯一标识
*/
private String openid;
/**
* 微信传递过来的随机字符串
*/
private String nonceStr;
/**
* 微信验证服务器地址的有效性时传递过来的随机字符串
*/
private String nonce;
/**
* 微信验证服务器地址的有效性时传递过来的回显字符串
*/
private String echostr;
/**
* 信验证服务器地址的有效性时传递过来的时间戳字符串
*/
private String timestamp;
/**
* 微信验证服务器地址的有效性时传递过来的签名字符串
*/
private String signature;

/**
* 接收来至微信服务器的消息
**/
@Action("receiveMessage")
public void receiveMessage() {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
@RequestMapping("receiveMessage")
public void receiveMessage(String signature, String timestamp, String nonce, String echostr, HttpServletRequest request, HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
String fromUserName = null;
Expand Down Expand Up @@ -115,60 +72,4 @@ public void receiveMessage() {
}
}

public String getAppid() {
return appid;
}

public String getSecret() {
return secret;
}

public String getOpenid() {
return openid;
}

public void setOpenid(String openid) {
this.openid = openid;
}

public String getNonceStr() {
return nonceStr;
}

public void setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}

public void setSignature(String signature) {
this.signature = signature;
}

public String getSignature() {
return signature;
}

public String getNonce() {
return nonce;
}

public void setNonce(String nonce) {
this.nonce = nonce;
}

public String getEchostr() {
return echostr;
}

public void setEchostr(String echostr) {
this.echostr = echostr;
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.mandy.wechatrobot.handle;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.mandy.wechatrobot.util.Constants;
import com.mandy.wechatrobot.util.MessageUtil;
import com.mandy.wechatrobot.vo.TextMessage;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* 处理事件类型消息
*/
@Service("eventMessageHandle")
@Service
public class EventMessageHandle implements WeChatMessageHandle {

@Override
Expand Down
Loading

0 comments on commit 6bf33e3

Please sign in to comment.