优化了代码

This commit is contained in:
jy.hu 2014-12-03 10:28:59 +08:00
parent d447775d3e
commit 01e68ec835
14 changed files with 43 additions and 74 deletions

View File

@ -20,8 +20,7 @@ import com.foxinmy.weixin4j.xml.XStream;
* @see com.foxinmy.weixin4j.action.WeixinAction
*/
@SuppressWarnings("unchecked")
public abstract class AbstractAction<M extends BaseMsg> implements
WeixinAction {
public abstract class AbstractAction<M extends BaseMsg> implements WeixinAction {
public abstract ResponseMessage execute(M inMessage);
@ -29,11 +28,7 @@ public abstract class AbstractAction<M extends BaseMsg> implements
public ResponseMessage execute(String msg) throws DocumentException {
BaseMsg message = MessageUtil.xml2msg(msg);
if (message == null) {
Class<M> messageClass = getGenericType();
XStream xstream = XStream.get();
xstream.processAnnotations(messageClass);
xstream.alias("xml", messageClass);
return execute(xstream.fromXML(msg, messageClass));
return execute(XStream.get(msg, getGenericType()));
}
return execute((M) message);
}

View File

@ -1,3 +1,3 @@
消息处理接口,与weixin4j-*-server配合使用
如果只使用API包,则可以不关注
如果单纯只使用API包,则可以不关注

View File

@ -2,39 +2,21 @@ package com.foxinmy.weixin4j.exception;
/**
* 调用微信支付抛出的异常
*
* @className PayException
* @author jy
* @date 2014年10月28日
* @since JDK 1.7
* @see
*/
public class PayException extends Exception {
public class PayException extends WeixinException {
private static final long serialVersionUID = 7148145661883468514L;
private String errorCode;
private String errorMsg;
public PayException(String errorMsg) {
super(errorMsg);
}
public PayException(String errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public PayException(String errorMsg) {
this.errorCode = "-1";
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return this.errorCode;
}
public String getErrorMsg() {
return this.errorMsg;
}
@Override
public String getMessage() {
return String.format("%s,%s", getErrorMsg(), getErrorCode());
super(errorCode, errorMsg);
}
}

View File

@ -22,7 +22,7 @@ public class WeixinException extends Exception {
}
public WeixinException(String errorMsg) {
this.errorCode = "";
this.errorCode = "-1";
this.errorMsg = errorMsg;
}

View File

@ -1,5 +1,12 @@
package com.foxinmy.weixin4j.model;
/**
* 常量类
* @className Consts
* @author jy
* @date 2014年12月3日
* @since JDK 1.7
* @see
*/
public final class Consts {
public static final String SUCCESS = "SUCCESS";
public static final String FAIL = "FAIL";

View File

@ -14,7 +14,8 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
* @date 2014年4月5日
* @since JDK 1.7
* @see <a
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">获取token</a>
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">微信公众平台获取token</a>
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号的主动模式</a>
*/
@XStreamAlias("app-token")
public class Token implements Serializable {

View File

@ -103,7 +103,7 @@ public abstract class WeixinAccount implements Serializable {
@Override
public String toString() {
return "WeixinAccount [id=" + id + ", secret=" + secret + ", token="
+ token + ", encodingAesKey=" + encodingAesKey + "]";
return "id=" + id + ", secret=" + secret + ", token=" + token
+ ", encodingAesKey=" + encodingAesKey;
}
}

View File

@ -171,8 +171,6 @@ public class WeixinMpAccount extends WeixinAccount {
+ partnerKey + ", mchId=" + mchId + ", deviceInfo="
+ deviceInfo + ", version=" + version + ", isAlive=" + isAlive
+ ", isService=" + isService + ", isSubscribe=" + isSubscribe
+ ", getId()=" + getId() + ", getSecret()=" + getSecret()
+ ", getToken()=" + getToken() + ", getEncodingAesKey()="
+ getEncodingAesKey() + "]";
+ ", " + super.toString() + "]";
}
}

View File

@ -26,9 +26,6 @@ public class WeixinQyAccount extends WeixinAccount {
@Override
public String toString() {
return "WeixinQyAccount [getTokenUrl()=" + getTokenUrl() + ", getId()="
+ getId() + ", getSecret()=" + getSecret() + ", getToken()="
+ getToken() + ", getEncodingAesKey()=" + getEncodingAesKey()
+ "]";
return "WeixinQyAccount [" + super.toString() + "]";
}
}

View File

@ -44,17 +44,8 @@ public class LocationEventMessage extends EventMessage {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[LocationEventMessage ,toUserName=").append(
super.getToUserName());
sb.append(" ,fromUserName=").append(super.getFromUserName());
sb.append(" ,msgType=").append(super.getMsgType());
sb.append(" ,eventType=").append(super.getEventType().name());
sb.append(" ,longitude=").append(longitude);
sb.append(" ,latitude=").append(latitude);
sb.append(" ,precision=").append(precision);
sb.append(" ,createTime=").append(super.getCreateTime());
sb.append(" ,msgId=").append(super.getMsgId()).append("]");
return sb.toString();
return "LocationEventMessage [latitude=" + latitude + ", longitude="
+ longitude + ", precision=" + precision + ", "
+ super.toString() + "]";
}
}

View File

@ -582,7 +582,7 @@ public class PayApi extends MpApi {
String param = map2xml(map);
response = request.post(refundquery_uri, param);
}
return new RefundConverter().fromXML(response.getAsString());
return RefundConverter.fromXML(response.getAsString());
}
/**

View File

@ -28,7 +28,7 @@ public class TmplApi extends MpApi {
/**
* 发送模板消息
*
* @param message
* @param message 消息对象
* @return 发送结果
* @throws WeixinException
* @see <a
@ -40,9 +40,8 @@ public class TmplApi extends MpApi {
throws WeixinException {
Token token = tokenHolder.getToken();
String template_send_uri = getRequestUri("template_send_uri");
String para = JSON.toJSONString(tplMessage);
Response response = request.post(
String.format(template_send_uri, token.getAccessToken()), para);
String.format(template_send_uri, token.getAccessToken()), JSON.toJSONString(tplMessage));
return response.getAsJsonResult();
}

View File

@ -56,7 +56,7 @@ public class PayUtil {
return createPayJsRequestJsonV3((PayPackageV3) payPackage,
weixinAccount);
}
throw new PayException("-1", "unknown pay");
throw new PayException("unknown pay");
}
/**

View File

@ -31,25 +31,26 @@ import com.thoughtworks.xstream.mapper.Mapper;
*/
public class RefundConverter {
private final static XStream xStream = XStream.get();
private final Mapper mapper;
private final ReflectionProvider reflectionProvider;
public RefundConverter() {
private final static Mapper mapper;
private final static ReflectionProvider reflectionProvider;
private final static Pattern pattern = Pattern.compile("(_\\d)$");
static {
xStream.processAnnotations(Refund.class);
xStream.registerConverter(new RefundConverter.$());
this.mapper = xStream.getMapper();
this.reflectionProvider = xStream.getReflectionProvider();
xStream.processAnnotations(RefundDetail.class);
xStream.registerConverter(new $());
mapper = xStream.getMapper();
reflectionProvider = xStream.getReflectionProvider();
}
public String toXML(Refund refund) {
public static String toXML(Refund refund) {
return xStream.toXML(refund);
}
public Refund fromXML(String xml) {
public static Refund fromXML(String xml) {
return xStream.fromXML(xml, Refund.class);
}
private class $ implements Converter {
private static class $ implements Converter {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
return clazz.equals(Refund.class);
@ -67,7 +68,6 @@ public class RefundConverter {
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
Refund refund = new Refund();
Pattern pattern = Pattern.compile("(_\\d)$");
Matcher matcher = null;
Map<String, Map<String, String>> outMap = new HashMap<String, Map<String, String>>();
while (reader.hasMoreChildren()) {
@ -108,9 +108,8 @@ public class RefundConverter {
detailXml.append("</").append(detailCanonicalName).append(">");
}
detailXml.append("</list>");
xStream.processAnnotations(RefundDetail.class);
refund.setDetails(xStream.fromXML(detailXml.toString(), List.class));
return refund;
}
}
}
}