最近在写 webService 接口,由于长时间没写过词类接口了 就把之前的知识忘掉了 结果可想而知 一顿操作猛如虎.......实际上是个小白鼠
今天在做数据监测时发现,返回值Json字符串 实体类属性值为null 时 会自动隐藏该属性;详情看下图:
原代码如下:
@WebMethod
public String getWjzJcNoDealtJcMsgs(){
List<WjzJcTsxx> list = new ArrayList<>();
list = jcTsxxMapper.selectAllNoCl();
String json = JSONArray.toJSONString(list);
return json ;
}
JSON 在转字符串时 会默认忽略到属性为null 的属性,所以导致返回值 属性为null时 字段丢失;
解决办法:指定Json 转换格式 正确代码参考如下:
@WebMethod
public String getWjzJcNoDealtJcMsgs(){
List<WjzJcTsxx> list = new ArrayList<>();
list = jcTsxxMapper.selectAllNoCl();
//使用日期格式化配置转换、允许值为null时以空字符串形式输出""
String json = JSONArray.toJSONString(list, SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteNullStringAsEmpty);
return json ;
}
源码解析:toJSONString() 方法是可变参数方法 ,参数可以设置很多属性,具体根据业务所需进行;
源代码所示:
//toJsonString 方法
public static String toJSONString(Object object, SerializerFeature... features) {
return toJSONString(object, DEFAULT_GENERATE_FEATURE, features);
}
/**
* 默认转换格式
*/
public static int DEFAULT_GENERATE_FEATURE;
static {
int features = 0;
features |= SerializerFeature.QuoteFieldNames.getMask();
features |= SerializerFeature.SkipTransientField.getMask();
features |= SerializerFeature.WriteEnumUsingName.getMask();
features |= SerializerFeature.SortField.getMask();
DEFAULT_GENERATE_FEATURE = features;
}
/**
* SerializerFeature 枚举类参数配置
*/
public enum SerializerFeature {
QuoteFieldNames,
/**
*
*/
UseSingleQuotes,
/**
*
*/
WriteMapNullValue,
/**
* 用枚举toString()值输出
*/
WriteEnumUsingToString,
/**
* 用枚举name()输出
*/
WriteEnumUsingName,
/**
*
*/
UseISO8601DateFormat,
/**
* @since 1.1
*/
WriteNullListAsEmpty,
/**
* @since 1.1
*/
WriteNullStringAsEmpty,
/**
* @since 1.1
*/
WriteNullNumberAsZero,
/**
* @since 1.1
*/
WriteNullBooleanAsFalse,
/**
* @since 1.1
*/
SkipTransientField,
/**
* @since 1.1
*/
SortField,
/**
* @since 1.1.1
*/
@Deprecated
WriteTabAsSpecial,
/**
* @since 1.1.2
*/
PrettyFormat,
/**
* @since 1.1.2
*/
WriteClassName,
/**
* @since 1.1.6
*/
DisableCircularReferenceDetect, // 32768
/**
* @since 1.1.9
*/
WriteSlashAsSpecial,
/**
* @since 1.1.10
*/
BrowserCompatible,
/**
* @since 1.1.14
*/
WriteDateUseDateFormat,
/**
* @since 1.1.15
*/
NotWriteRootClassName,
/**
* @since 1.1.19
* @deprecated
*/
DisableCheckSpecialChar,
/**
* @since 1.1.35
*/
BeanToArray,
/**
* @since 1.1.37
*/
WriteNonStringKeyAsString,
/**
* @since 1.1.42
*/
NotWriteDefaultValue,
/**
* @since 1.2.6
*/
BrowserSecure,
/**
* @since 1.2.7
*/
IgnoreNonFieldGetter,
/**
* @since 1.2.9
*/
WriteNonStringValueAsString,
/**
* @since 1.2.11
*/
IgnoreErrorGetter,
/**
* @since 1.2.16
*/
WriteBigDecimalAsPlain
;
SerializerFeature(){
mask = (1 << ordinal());
}
public final int mask;
public final int getMask() {
return mask;
}
public static boolean isEnabled(int features, SerializerFeature feature) {
return (features & feature.mask) != 0;
}
public static boolean isEnabled(int features, int fieaturesB, SerializerFeature feature) {
int mask = feature.mask;
return (features & mask) != 0 || (fieaturesB & mask) != 0;
}
public static int config(int features, SerializerFeature feature, boolean state) {
if (state) {
features |= feature.mask;
} else {
features &= ~feature.mask;
}
return features;
}
public static int of(SerializerFeature[] features) {
if (features == null) {
return 0;
}
int value = 0;
for (SerializerFeature feature: features) {
value |= feature.mask;
}
return value;
}
public final static SerializerFeature[] EMPTY = new SerializerFeature[0];
public static final int WRITE_MAP_NULL_FEATURES
= WriteMapNullValue.getMask()
| WriteNullBooleanAsFalse.getMask()
| WriteNullListAsEmpty.getMask()
| WriteNullNumberAsZero.getMask()
| WriteNullStringAsEmpty.getMask()
;
}
特此备注 留以后用。