mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-06-28 13:16:19 +08:00
优化部分代码
This commit is contained in:
parent
afb5e6151f
commit
8d6cfce13c
@ -43,15 +43,20 @@ public class BeanUtils {
|
||||
boolean isAccessible = field.isAccessible();
|
||||
field.setAccessible(true);
|
||||
if (field.isAnnotationPresent(Required.class)) {
|
||||
if (field.get(bean) == null || (field.get(bean) instanceof String && StringUtils.isBlank(field.get(bean).toString()))) {
|
||||
//两种情况,一种是值为null,另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值
|
||||
// 两种情况,一种是值为null,
|
||||
// 另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值
|
||||
boolean isRequiredMissing = field.get(bean) == null
|
||||
|| (field.get(bean) instanceof String
|
||||
&& StringUtils.isBlank(field.get(bean).toString())
|
||||
);
|
||||
if (isRequiredMissing) {
|
||||
requiredFields.add(field.getName());
|
||||
}
|
||||
}
|
||||
field.setAccessible(isAccessible);
|
||||
} catch (SecurityException | IllegalArgumentException
|
||||
| IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,11 +88,13 @@ public class BeanUtils {
|
||||
|
||||
if (field.isAnnotationPresent(XStreamAlias.class)) {
|
||||
result.put(field.getAnnotation(XStreamAlias.class).value(), field.get(bean).toString());
|
||||
} else {
|
||||
result.put(field.getName(), field.get(bean).toString());
|
||||
}
|
||||
|
||||
field.setAccessible(isAccessible);
|
||||
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,14 +7,10 @@ import me.chanjar.weixin.common.util.http.HttpType;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import okhttp3.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
protected OkHttpClient httpClient;
|
||||
protected OkHttpProxyInfo httpProxy;
|
||||
|
||||
@ -36,7 +32,7 @@ public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient,
|
||||
|
||||
@Override
|
||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||
logger.debug("WxCpServiceOkHttpImpl is running");
|
||||
this.log.debug("WxCpServiceOkHttpImpl is running");
|
||||
if (this.configStorage.isAccessTokenExpired() || forceRefresh) {
|
||||
synchronized (this.globalAccessTokenRefreshLock) {
|
||||
if (this.configStorage.isAccessTokenExpired()) {
|
||||
@ -47,18 +43,14 @@ public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient,
|
||||
OkHttpClient client = getRequestHttpClient();
|
||||
//请求的request
|
||||
Request request = new Request.Builder().url(url).get().build();
|
||||
Response response = null;
|
||||
try {
|
||||
response = client.newCall(request).execute();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String resultContent = null;
|
||||
try {
|
||||
Response response = client.newCall(request).execute();
|
||||
resultContent = response.body().string();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
this.log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
WxError error = WxError.fromJson(resultContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
@ -74,7 +66,7 @@ public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient,
|
||||
|
||||
@Override
|
||||
public void initHttp() {
|
||||
logger.debug("WxCpServiceOkHttpImpl initHttp");
|
||||
this.log.debug("WxCpServiceOkHttpImpl initHttp");
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
|
||||
//设置代理
|
||||
if (httpProxy != null) {
|
||||
|
@ -9,8 +9,6 @@ import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -22,8 +20,6 @@ import java.util.UUID;
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxMaMediaServiceImpl implements WxMaMediaService {
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private WxMaService wxMaService;
|
||||
|
||||
public WxMaMediaServiceImpl(WxMaService wxMaService) {
|
||||
@ -35,8 +31,7 @@ public class WxMaMediaServiceImpl implements WxMaMediaService {
|
||||
try {
|
||||
return this.uploadMedia(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,8 +48,7 @@ public class WxMaMediaServiceImpl implements WxMaMediaService {
|
||||
.create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("wxma").toFile());
|
||||
return this.wxMaService.execute(executor, MEDIA_GET_URL, "media_id=" + mediaId);
|
||||
} catch (IOException e) {
|
||||
this.log.error(e.getMessage(), e);
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,8 +37,7 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
try {
|
||||
return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,16 +8,11 @@ import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import okhttp3.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private OkHttpClient httpClient;
|
||||
private OkHttpProxyInfo httpProxy;
|
||||
|
||||
@ -38,7 +33,7 @@ public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient,
|
||||
|
||||
@Override
|
||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||
logger.debug("WxMpServiceOkHttpImpl is running");
|
||||
this.log.debug("WxMpServiceOkHttpImpl is running");
|
||||
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||
try {
|
||||
lock.lock();
|
||||
@ -59,7 +54,7 @@ public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient,
|
||||
accessToken.getExpiresIn());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
this.log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
@ -68,7 +63,7 @@ public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient,
|
||||
|
||||
@Override
|
||||
public void initHttp() {
|
||||
logger.debug("WxMpServiceOkHttpImpl initHttp");
|
||||
this.log.debug("WxMpServiceOkHttpImpl initHttp");
|
||||
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();
|
||||
|
||||
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
|
||||
|
@ -190,7 +190,7 @@ public abstract class WxPayBaseRequest {
|
||||
* 3、生成签名,并设置进去
|
||||
* </pre>
|
||||
*
|
||||
* @param config 支付配置对象,用于读取相应系统配置信息
|
||||
* @param config 支付配置对象,用于读取相应系统配置信息
|
||||
* @param isIgnoreSignType 签名时,是否忽略signType
|
||||
*/
|
||||
public void checkAndSign(WxPayConfig config, boolean isIgnoreSignType) throws WxPayException {
|
||||
|
@ -6,6 +6,8 @@ import me.chanjar.weixin.common.util.BeanUtils;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
@ -23,11 +25,12 @@ import java.util.TreeMap;
|
||||
* </pre>
|
||||
*/
|
||||
public class SignUtils {
|
||||
private static final Logger log = LoggerFactory.getLogger(SignUtils.class);
|
||||
|
||||
/**
|
||||
* 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3)
|
||||
*
|
||||
* @param xmlBean Bean需要标记有XML注解
|
||||
* @param xmlBean Bean里的属性如果存在XML注解,则使用其作为key,否则使用变量名
|
||||
* @param signType 签名类型,如果为空,则默认为MD5
|
||||
* @param signKey 签名Key
|
||||
* @param isIgnoreSignType 签名时,是否忽略signType
|
||||
@ -81,7 +84,7 @@ public class SignUtils {
|
||||
byte[] bytes = hmacSHA256.doFinal(message.getBytes());
|
||||
return Hex.encodeHexString(bytes).toUpperCase();
|
||||
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -67,7 +67,7 @@ public class WxPayServiceAbstractImplTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOrder_jssdk() throws Exception {
|
||||
public void testCreateOrder_jsapi() throws Exception {
|
||||
WxPayMpOrderResult result = this.payService
|
||||
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
|
||||
.body("我去")
|
||||
|
Loading…
Reference in New Issue
Block a user