mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-19 18:22:27 +08:00
引入自定义ToString工具类和方法,便于查看部分bean对象值
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package me.chanjar.weixin.common.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
import me.chanjar.weixin.common.util.ToStringUtils;
|
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 卡券Api签名
|
||||
@@ -35,7 +34,7 @@ public class WxCardApiSignature implements Serializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
|
||||
return ToStringUtils.toSimpleString(this);
|
||||
}
|
||||
|
||||
public String getAppId() {
|
||||
|
@@ -1,11 +1,10 @@
|
||||
package me.chanjar.weixin.common.bean.menu;
|
||||
|
||||
import me.chanjar.weixin.common.util.ToStringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
|
||||
public class WxMenuButton {
|
||||
|
||||
private String type;
|
||||
@@ -18,10 +17,9 @@ public class WxMenuButton {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this,
|
||||
ToStringStyle.MULTI_LINE_STYLE);
|
||||
return ToStringUtils.toSimpleString(this);
|
||||
}
|
||||
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
@@ -69,4 +67,4 @@ public class WxMenuButton {
|
||||
public void setMediaId(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package me.chanjar.weixin.common.bean.menu;
|
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import me.chanjar.weixin.common.util.ToStringUtils;
|
||||
|
||||
public class WxMenuRule {
|
||||
private String tagId;
|
||||
@@ -70,6 +69,6 @@ public class WxMenuRule {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
|
||||
return ToStringUtils.toSimpleString(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,98 +0,0 @@
|
||||
package me.chanjar.weixin.common.util;
|
||||
|
||||
/**
|
||||
* copy from apache-commons-lang3
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
/**
|
||||
* <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
|
||||
* <p>
|
||||
* <pre>
|
||||
* StringUtils.isBlank(null) = true
|
||||
* StringUtils.isBlank("") = true
|
||||
* StringUtils.isBlank(" ") = true
|
||||
* StringUtils.isBlank("bob") = false
|
||||
* StringUtils.isBlank(" bob ") = false
|
||||
* </pre>
|
||||
*
|
||||
* @param cs the CharSequence to check, may be null
|
||||
* @return {@code true} if the CharSequence is null, empty or whitespace
|
||||
* @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
|
||||
*/
|
||||
public static boolean isBlank(CharSequence cs) {
|
||||
int strLen;
|
||||
if (cs == null || (strLen = cs.length()) == 0) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if (Character.isWhitespace(cs.charAt(i)) == false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
|
||||
* <p>
|
||||
* <pre>
|
||||
* StringUtils.isNotBlank(null) = false
|
||||
* StringUtils.isNotBlank("") = false
|
||||
* StringUtils.isNotBlank(" ") = false
|
||||
* StringUtils.isNotBlank("bob") = true
|
||||
* StringUtils.isNotBlank(" bob ") = true
|
||||
* </pre>
|
||||
*
|
||||
* @param cs the CharSequence to check, may be null
|
||||
* @return {@code true} if the CharSequence is
|
||||
* not empty and not null and not whitespace
|
||||
* @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
|
||||
*/
|
||||
public static boolean isNotBlank(CharSequence cs) {
|
||||
return !StringUtils.isBlank(cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if a CharSequence is empty ("") or null.</p>
|
||||
* <p>
|
||||
* <pre>
|
||||
* StringUtils.isEmpty(null) = true
|
||||
* StringUtils.isEmpty("") = true
|
||||
* StringUtils.isEmpty(" ") = false
|
||||
* StringUtils.isEmpty("bob") = false
|
||||
* StringUtils.isEmpty(" bob ") = false
|
||||
* </pre>
|
||||
* <p>
|
||||
* <p>NOTE: This method changed in Lang version 2.0.
|
||||
* It no longer trims the CharSequence.
|
||||
* That functionality is available in isBlank().</p>
|
||||
*
|
||||
* @param cs the CharSequence to check, may be null
|
||||
* @return {@code true} if the CharSequence is empty or null
|
||||
* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
|
||||
*/
|
||||
public static boolean isEmpty(CharSequence cs) {
|
||||
return cs == null || cs.length() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if a CharSequence is not empty ("") and not null.</p>
|
||||
* <p>
|
||||
* <pre>
|
||||
* StringUtils.isNotEmpty(null) = false
|
||||
* StringUtils.isNotEmpty("") = false
|
||||
* StringUtils.isNotEmpty(" ") = true
|
||||
* StringUtils.isNotEmpty("bob") = true
|
||||
* StringUtils.isNotEmpty(" bob ") = true
|
||||
* </pre>
|
||||
*
|
||||
* @param cs the CharSequence to check, may be null
|
||||
* @return {@code true} if the CharSequence is not empty and not null
|
||||
* @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
|
||||
*/
|
||||
public static boolean isNotEmpty(CharSequence cs) {
|
||||
return !StringUtils.isEmpty(cs);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
package me.chanjar.weixin.common.util;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 自定义的ToString方法,用于产生去掉空值属性的字符串
|
||||
* Created by Binary Wang on 2016-10-27.
|
||||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||
* </pre>
|
||||
*/
|
||||
public class ToStringUtils {
|
||||
public static final ToStringStyle THE_STYLE = new SimpleMultiLineToStringStyle();
|
||||
private static class SimpleMultiLineToStringStyle extends ToStringStyle {
|
||||
private static final long serialVersionUID = 4645306494220335355L;
|
||||
private static final String LINE_SEPARATOR = "\n";
|
||||
private static final String NULL_TEXT = "<null>";
|
||||
|
||||
public SimpleMultiLineToStringStyle() {
|
||||
super();
|
||||
this.setContentStart("[");
|
||||
this.setFieldSeparator(LINE_SEPARATOR + " ");
|
||||
this.setFieldSeparatorAtStart(true);
|
||||
this.setContentEnd(LINE_SEPARATOR + "]");
|
||||
this.setNullText(NULL_TEXT);
|
||||
this.setUseShortClassName(true);
|
||||
this.setUseIdentityHashCode(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于产生去掉空值属性并以换行符分割各属性键值的toString字符串
|
||||
* @param obj
|
||||
*/
|
||||
public static String toSimpleString(Object obj) {
|
||||
String toStringResult = ToStringBuilder.reflectionToString(obj, THE_STYLE);
|
||||
String[] split = toStringResult.split(SimpleMultiLineToStringStyle.LINE_SEPARATOR);
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (String string : split) {
|
||||
if (string.endsWith(SimpleMultiLineToStringStyle.NULL_TEXT)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.append(string + SimpleMultiLineToStringStyle.LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
if (result.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
//如果没有非空的属性,就输出 <all null properties>
|
||||
if (StringUtils.countMatches(result, SimpleMultiLineToStringStyle.LINE_SEPARATOR) == 2) {
|
||||
return result.toString().split(SimpleMultiLineToStringStyle.LINE_SEPARATOR)[0]
|
||||
+ "<all null values>]";
|
||||
}
|
||||
|
||||
return result.deleteCharAt(result.length() - 1).toString();
|
||||
}
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import me.chanjar.weixin.common.util.StringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.annotation.NotThreadSafe;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
|
@@ -2,8 +2,8 @@ package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.StringUtils;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import me.chanjar.weixin.common.util.StringUtils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
|
Reference in New Issue
Block a user