使用lombok的@Data注解简化common模块的所有bean类

This commit is contained in:
Binary Wang
2017-11-14 11:05:18 +08:00
parent e007a13df6
commit 993999735b
48 changed files with 131 additions and 426 deletions

View File

@@ -1,9 +1,11 @@
package me.chanjar.weixin.common.bean;
import lombok.Data;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.io.Serializable;
@Data
public class WxAccessToken implements Serializable {
private static final long serialVersionUID = 8709719312922168909L;
@@ -15,20 +17,4 @@ public class WxAccessToken implements Serializable {
return WxGsonBuilder.create().fromJson(json, WxAccessToken.class);
}
public String getAccessToken() {
return this.accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public int getExpiresIn() {
return this.expiresIn;
}
public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}
}

View File

@@ -1,17 +1,18 @@
package me.chanjar.weixin.common.bean;
import lombok.Data;
import me.chanjar.weixin.common.util.ToStringUtils;
import java.io.Serializable;
/**
* 卡券Api签名
* 卡券Api签名.
*
* @author YuJian
* @version 15/11/8
*/
@Data
public class WxCardApiSignature implements Serializable {
private static final long serialVersionUID = 158176707226975979L;
private String appId;
@@ -37,75 +38,4 @@ public class WxCardApiSignature implements Serializable {
return ToStringUtils.toSimpleString(this);
}
public String getAppId() {
return this.appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getCardId() {
return this.cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getCardType() {
return this.cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getLocationId() {
return this.locationId;
}
public void setLocationId(String locationId) {
this.locationId = locationId;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getOpenId() {
return this.openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public Long getTimestamp() {
return this.timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public String getNonceStr() {
return this.nonceStr;
}
public void setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
}
public String getSignature() {
return this.signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
}

View File

@@ -1,10 +1,13 @@
package me.chanjar.weixin.common.bean;
import lombok.Data;
import java.io.Serializable;
/**
* jspai signature
* jspai signature.
*/
@Data
public class WxJsapiSignature implements Serializable {
private static final long serialVersionUID = -1116808193154384804L;
@@ -18,43 +21,4 @@ public class WxJsapiSignature implements Serializable {
private String signature;
public String getSignature() {
return this.signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getNonceStr() {
return nonceStr;
}
public void setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
}
public long getTimestamp() {
return this.timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
}

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.common.bean.menu;
import lombok.Data;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
@@ -15,8 +16,8 @@ import java.util.List;
*
* @author Daniel Qian
*/
@Data
public class WxMenu implements Serializable {
private static final long serialVersionUID = -7083914585539687746L;
private List<WxMenuButton> buttons = new ArrayList<>();
@@ -40,22 +41,6 @@ public class WxMenu implements Serializable {
.fromJson(new InputStreamReader(is, StandardCharsets.UTF_8), WxMenu.class);
}
public List<WxMenuButton> getButtons() {
return this.buttons;
}
public void setButtons(List<WxMenuButton> buttons) {
this.buttons = buttons;
}
public WxMenuRule getMatchRule() {
return this.matchRule;
}
public void setMatchRule(WxMenuRule matchRule) {
this.matchRule = matchRule;
}
public String toJson() {
return WxGsonBuilder.create().toJson(this);
}

View File

@@ -1,12 +1,14 @@
package me.chanjar.weixin.common.bean.menu;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.common.util.ToStringUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
public class WxMenuButton implements Serializable {
private static final long serialVersionUID = -1070939403109776555L;
@@ -77,67 +79,4 @@ public class WxMenuButton implements Serializable {
return ToStringUtils.toSimpleString(this);
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return this.key;
}
public void setKey(String key) {
this.key = key;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public List<WxMenuButton> getSubButtons() {
return this.subButtons;
}
public void setSubButtons(List<WxMenuButton> subButtons) {
this.subButtons = subButtons;
}
public String getMediaId() {
return this.mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getPagePath() {
return pagePath;
}
public void setPagePath(String pagePath) {
this.pagePath = pagePath;
}
}

View File

@@ -1,10 +1,12 @@
package me.chanjar.weixin.common.bean.menu;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.common.util.ToStringUtils;
import java.io.Serializable;
@Data
public class WxMenuRule implements Serializable {
private static final long serialVersionUID = -4587181819499286670L;
@@ -20,62 +22,6 @@ public class WxMenuRule implements Serializable {
private String clientPlatformType;
private String language;
public String getTagId() {
return this.tagId;
}
public void setTagId(String tagId) {
this.tagId = tagId;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return this.province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getClientPlatformType() {
return this.clientPlatformType;
}
public void setClientPlatformType(String clientPlatformType) {
this.clientPlatformType = clientPlatformType;
}
public String getLanguage() {
return this.language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public String toString() {
return ToStringUtils.toSimpleString(this);

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.common.bean.result;
import lombok.Builder;
import lombok.Data;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.io.Serializable;
@@ -9,6 +11,8 @@ import java.io.Serializable;
*
* @author Daniel Qian
*/
@Data
@Builder
public class WxError implements Serializable {
private static final long serialVersionUID = 7869786563361406291L;
@@ -23,34 +27,6 @@ public class WxError implements Serializable {
return WxGsonBuilder.create().fromJson(json, WxError.class);
}
public static Builder newBuilder() {
return new Builder();
}
public int getErrorCode() {
return this.errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return this.errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getJson() {
return this.json;
}
public void setJson(String json) {
this.json = json;
}
@Override
public String toString() {
if (this.json != null) {
@@ -59,29 +35,4 @@ public class WxError implements Serializable {
return "错误: Code=" + this.errorCode + ", Msg=" + this.errorMsg;
}
public static class Builder {
private int errorCode;
private String errorMsg;
public Builder setErrorCode(int errorCode) {
this.errorCode = errorCode;
return this;
}
public Builder setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
return this;
}
/**
* 构造器方法.
*/
public WxError build() {
WxError wxError = new WxError();
wxError.setErrorCode(this.errorCode);
wxError.setErrorMsg(this.errorMsg);
return wxError;
}
}
}

View File

@@ -1,10 +1,12 @@
package me.chanjar.weixin.common.bean.result;
import lombok.Data;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.io.Serializable;
@Data
public class WxMediaUploadResult implements Serializable {
private static final long serialVersionUID = 330834334738622341L;
@@ -17,38 +19,6 @@ public class WxMediaUploadResult implements Serializable {
return WxGsonBuilder.create().fromJson(json, WxMediaUploadResult.class);
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public String getMediaId() {
return this.mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
public long getCreatedAt() {
return this.createdAt;
}
public void setCreatedAt(long createdAt) {
this.createdAt = createdAt;
}
public String getThumbMediaId() {
return this.thumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
this.thumbMediaId = thumbMediaId;
}
@Override
public String toString() {
return ToStringUtils.toSimpleString(this);

View File

@@ -64,7 +64,7 @@ public class BeanUtils {
if (!requiredFields.isEmpty()) {
String msg = "必填字段 " + requiredFields + " 必须提供值";
log.debug(msg);
throw new WxErrorException(WxError.newBuilder().setErrorMsg(msg).build());
throw new WxErrorException(WxError.builder().errorMsg(msg).build());
}
}

View File

@@ -19,6 +19,8 @@ import java.util.regex.Pattern;
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class HttpResponseProxy {
private static final Pattern PATTERN = Pattern.compile(".*filename=\"(.*)\"");
private CloseableHttpResponse apacheHttpResponse;
private HttpResponse joddHttpResponse;
private Response okHttpResponse;
@@ -56,7 +58,7 @@ public class HttpResponseProxy {
private String getFileName(CloseableHttpResponse response) throws WxErrorException {
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
if (contentDispositionHeader == null || contentDispositionHeader.length == 0) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
throw new WxErrorException(WxError.builder().errorMsg("无法获取到文件名").build());
}
return this.extractFileNameFromContentString(contentDispositionHeader[0].getValue());
@@ -74,15 +76,15 @@ public class HttpResponseProxy {
private String extractFileNameFromContentString(String content) throws WxErrorException {
if (content == null || content.length() == 0) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
throw new WxErrorException(WxError.builder().errorMsg("无法获取到文件名").build());
}
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
Matcher m = p.matcher(content);
Matcher m = PATTERN.matcher(content);
if (m.matches()) {
return m.group(1);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
throw new WxErrorException(WxError.builder().errorMsg("无法获取到文件名").build());
}
}

View File

@@ -39,9 +39,7 @@ public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<C
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
if (responseContent.isEmpty()) {
throw new WxErrorException(
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
.build());
throw new WxErrorException(WxError.builder().errorCode(9999).errorMsg("无响应内容").build());
}
if (responseContent.startsWith("<xml>")) {

View File

@@ -5,7 +5,6 @@ import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.StringPool;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
@@ -40,9 +39,8 @@ public class JoddHttpSimplePostRequestExecutor extends SimplePostRequestExecutor
String responseContent = response.bodyText();
if (responseContent.isEmpty()) {
throw new WxErrorException(
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
.build());
throw new WxErrorException(WxError.builder().errorCode(9999).errorMsg("无响应内容")
.build());
}
if (responseContent.startsWith("<xml>")) {

View File

@@ -14,23 +14,26 @@ import me.chanjar.weixin.common.bean.result.WxError;
import java.lang.reflect.Type;
/**
* @author Daniel Qian
* @author Daniel Qian.
*/
public class WxErrorAdapter implements JsonDeserializer<WxError> {
@Override
public WxError deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
WxError wxError = new WxError();
public WxError deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
WxError.WxErrorBuilder errorBuilder = WxError.builder();
JsonObject wxErrorJsonObject = json.getAsJsonObject();
if (wxErrorJsonObject.get("errcode") != null && !wxErrorJsonObject.get("errcode").isJsonNull()) {
wxError.setErrorCode(GsonHelper.getAsPrimitiveInt(wxErrorJsonObject.get("errcode")));
errorBuilder.errorCode(GsonHelper.getAsPrimitiveInt(wxErrorJsonObject.get("errcode")));
}
if (wxErrorJsonObject.get("errmsg") != null && !wxErrorJsonObject.get("errmsg").isJsonNull()) {
wxError.setErrorMsg(GsonHelper.getAsString(wxErrorJsonObject.get("errmsg")));
errorBuilder.errorMsg(GsonHelper.getAsString(wxErrorJsonObject.get("errmsg")));
}
wxError.setJson(json.toString());
return wxError;
errorBuilder.json(json.toString());
return errorBuilder.build();
}
}

View File

@@ -8,7 +8,6 @@ import org.testng.annotations.*;
public class WxErrorTest {
public void testFromJson() {
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\" }";
WxError wxError = WxError.fromJson(json);
Assert.assertTrue(wxError.getErrorCode() == 40003);