Skip to content

Commit 8d6cfce

Browse files
committed
优化部分代码
1 parent afb5e61 commit 8d6cfce

File tree

8 files changed

+29
-39
lines changed

8 files changed

+29
-39
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/BeanUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,20 @@ public static void checkRequiredFields(Object bean) throws WxErrorException {
4343
boolean isAccessible = field.isAccessible();
4444
field.setAccessible(true);
4545
if (field.isAnnotationPresent(Required.class)) {
46-
if (field.get(bean) == null || (field.get(bean) instanceof String && StringUtils.isBlank(field.get(bean).toString()))) {
47-
//两种情况,一种是值为null,另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值
46+
// 两种情况,一种是值为null,
47+
// 另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值
48+
boolean isRequiredMissing = field.get(bean) == null
49+
|| (field.get(bean) instanceof String
50+
&& StringUtils.isBlank(field.get(bean).toString())
51+
);
52+
if (isRequiredMissing) {
4853
requiredFields.add(field.getName());
4954
}
5055
}
5156
field.setAccessible(isAccessible);
5257
} catch (SecurityException | IllegalArgumentException
5358
| IllegalAccessException e) {
54-
e.printStackTrace();
59+
log.error(e.getMessage(), e);
5560
}
5661
}
5762

@@ -83,11 +88,13 @@ public static Map<String, String> xmlBean2Map(Object bean) {
8388

8489
if (field.isAnnotationPresent(XStreamAlias.class)) {
8590
result.put(field.getAnnotation(XStreamAlias.class).value(), field.get(bean).toString());
91+
} else {
92+
result.put(field.getName(), field.get(bean).toString());
8693
}
8794

8895
field.setAccessible(isAccessible);
8996
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
90-
e.printStackTrace();
97+
log.error(e.getMessage(), e);
9198
}
9299

93100
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
88
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
99
import okhttp3.*;
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
1210

1311
import java.io.IOException;
1412

1513
public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {
16-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
17-
1814
protected OkHttpClient httpClient;
1915
protected OkHttpProxyInfo httpProxy;
2016

@@ -36,7 +32,7 @@ public HttpType getRequestType() {
3632

3733
@Override
3834
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
39-
logger.debug("WxCpServiceOkHttpImpl is running");
35+
this.log.debug("WxCpServiceOkHttpImpl is running");
4036
if (this.configStorage.isAccessTokenExpired() || forceRefresh) {
4137
synchronized (this.globalAccessTokenRefreshLock) {
4238
if (this.configStorage.isAccessTokenExpired()) {
@@ -47,18 +43,14 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
4743
OkHttpClient client = getRequestHttpClient();
4844
//请求的request
4945
Request request = new Request.Builder().url(url).get().build();
50-
Response response = null;
51-
try {
52-
response = client.newCall(request).execute();
53-
} catch (IOException e) {
54-
e.printStackTrace();
55-
}
5646
String resultContent = null;
5747
try {
48+
Response response = client.newCall(request).execute();
5849
resultContent = response.body().string();
5950
} catch (IOException e) {
60-
e.printStackTrace();
51+
this.log.error(e.getMessage(), e);
6152
}
53+
6254
WxError error = WxError.fromJson(resultContent);
6355
if (error.getErrorCode() != 0) {
6456
throw new WxErrorException(error);
@@ -74,7 +66,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
7466

7567
@Override
7668
public void initHttp() {
77-
logger.debug("WxCpServiceOkHttpImpl initHttp");
69+
this.log.debug("WxCpServiceOkHttpImpl initHttp");
7870
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
7971
//设置代理
8072
if (httpProxy != null) {

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaMediaServiceImpl.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
1010
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
1111
import me.chanjar.weixin.common.util.http.RequestExecutor;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
1412

1513
import java.io.File;
1614
import java.io.IOException;
@@ -22,8 +20,6 @@
2220
* @author <a href="https://github.com/binarywang">Binary Wang</a>
2321
*/
2422
public class WxMaMediaServiceImpl implements WxMaMediaService {
25-
private final Logger log = LoggerFactory.getLogger(this.getClass());
26-
2723
private WxMaService wxMaService;
2824

2925
public WxMaMediaServiceImpl(WxMaService wxMaService) {
@@ -35,8 +31,7 @@ public WxMediaUploadResult uploadMedia(String mediaType, String fileType, InputS
3531
try {
3632
return this.uploadMedia(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
3733
} catch (IOException e) {
38-
e.printStackTrace();
39-
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
34+
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
4035
}
4136
}
4237

@@ -53,8 +48,7 @@ public File getMedia(String mediaId) throws WxErrorException {
5348
.create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("wxma").toFile());
5449
return this.wxMaService.execute(executor, MEDIA_GET_URL, "media_id=" + mediaId);
5550
} catch (IOException e) {
56-
this.log.error(e.getMessage(), e);
57-
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
51+
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
5852
}
5953
}
6054

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputS
3737
try {
3838
return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
3939
} catch (IOException e) {
40-
e.printStackTrace();
41-
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
40+
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
4241
}
4342
}
4443

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceOkHttpImpl.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
99
import me.chanjar.weixin.mp.api.WxMpService;
1010
import okhttp3.*;
11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
1311

1412
import java.io.IOException;
1513
import java.util.concurrent.locks.Lock;
1614

1715
public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {
18-
19-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
20-
2116
private OkHttpClient httpClient;
2217
private OkHttpProxyInfo httpProxy;
2318

@@ -38,7 +33,7 @@ public HttpType getRequestType() {
3833

3934
@Override
4035
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
41-
logger.debug("WxMpServiceOkHttpImpl is running");
36+
this.log.debug("WxMpServiceOkHttpImpl is running");
4237
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
4338
try {
4439
lock.lock();
@@ -59,7 +54,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
5954
accessToken.getExpiresIn());
6055
}
6156
} catch (IOException e) {
62-
e.printStackTrace();
57+
this.log.error(e.getMessage(), e);
6358
} finally {
6459
lock.unlock();
6560
}
@@ -68,7 +63,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
6863

6964
@Override
7065
public void initHttp() {
71-
logger.debug("WxMpServiceOkHttpImpl initHttp");
66+
this.log.debug("WxMpServiceOkHttpImpl initHttp");
7267
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();
7368

7469
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public String toXML() {
190190
* 3、生成签名,并设置进去
191191
* </pre>
192192
*
193-
* @param config 支付配置对象,用于读取相应系统配置信息
193+
* @param config 支付配置对象,用于读取相应系统配置信息
194194
* @param isIgnoreSignType 签名时,是否忽略signType
195195
*/
196196
public void checkAndSign(WxPayConfig config, boolean isIgnoreSignType) throws WxPayException {

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/util/SignUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.apache.commons.codec.binary.Hex;
77
import org.apache.commons.codec.digest.DigestUtils;
88
import org.apache.commons.lang3.StringUtils;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
911

1012
import javax.crypto.Mac;
1113
import javax.crypto.spec.SecretKeySpec;
@@ -23,11 +25,12 @@
2325
* </pre>
2426
*/
2527
public class SignUtils {
28+
private static final Logger log = LoggerFactory.getLogger(SignUtils.class);
2629

2730
/**
2831
* 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3)
2932
*
30-
* @param xmlBean Bean需要标记有XML注解
33+
* @param xmlBean Bean里的属性如果存在XML注解,则使用其作为key,否则使用变量名
3134
* @param signType 签名类型,如果为空,则默认为MD5
3235
* @param signKey 签名Key
3336
* @param isIgnoreSignType 签名时,是否忽略signType
@@ -81,7 +84,7 @@ private static String createHMACSha256Sign(String message, String key) {
8184
byte[] bytes = hmacSHA256.doFinal(message.getBytes());
8285
return Hex.encodeHexString(bytes).toUpperCase();
8386
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
84-
e.printStackTrace();
87+
log.error(e.getMessage(), e);
8588
}
8689

8790
return null;

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testCreateOrder() throws Exception {
6767
}
6868

6969
@Test
70-
public void testCreateOrder_jssdk() throws Exception {
70+
public void testCreateOrder_jsapi() throws Exception {
7171
WxPayMpOrderResult result = this.payService
7272
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
7373
.body("我去")

0 commit comments

Comments
 (0)