SpringBoot常用配置及代码
用于将返回实体中为null的字段转为空字符串
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@Configuration
public class JacksonConfig extends WebMvcConfigurationSupport {
/**
* 实体中的null 替换为空字符串返回
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFeatures(SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty,//字符串null返回空字符串
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.PrettyFormat);
converters.add(converter);
}
}
附上例:fastjson SerializerFeature详解
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
SerializerFeature属性
| 名称 | 含义 | 默认值 |
|---|---|---|
| QuoteFieldNames | 输出key时是否使用双引号 | true |
| UseSingleQuotes | 使用单引号而不是双引号 | false |
| WriteMapNullValue | 是否输出值为null的字段 | false |
| WriteEnumUsingToString | Enum输出name()或者original | false |
| UseISO8601DateFormat | Date使用ISO8601格式输出 | false |
| WriteNullListAsEmpty | List字段如果为null,输出为[],而非null | |
| WriteNullStringAsEmpty | 字符类型字段如果为null,输出为”“,而非null | |
| WriteNullNumberAsZero | 数值字段如果为null,输出为0,而非null | |
| WriteNullBooleanAsFalse | Boolean字段如果为null,输出为false,而非null | |
| SkipTransientField | 如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略 | true |
| SortField | 按字段名称排序后输出 | false |
| WriteTabAsSpecial | 把\t做转义输出(不推荐) | false |
| PrettyFormat | 结果是否格式化 | false |
| WriteClassName | 序列化时写入类型信息,默认为false。反序列化是需用到 | |
| DisableCircularReferenceDetect | 消除对同一对象循环引用的问题 | false |
| WriteSlashAsSpecial | 对斜杠’/’进行转义 | |
| BrowserCompatible | 将中文都会序列化为\uXXXX格式,字节数会多一些,但是能兼容IE 6 | false |
| WriteDateUseDateFormat | 全局修改日期格式,JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat); | false |
| DisableCheckSpecialChar | 一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性 | false |
| NotWriteRootClassName | ||
| BeanToArray | 将对象转为array输出 | |
| WriteNonStringKeyAsString | ||
| NotWriteDefaultValue | ||
| BrowserSecure | ||
| IgnoreNonFieldGetter | ||
| WriteEnumUsingName |
list递归组建树形结构
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.List;
/**
* parentId为当前List最上层父id
* idKey为实体类对象中id键名
* parentKey为实体类对象中id的键名
* childName为返回数据子列表的命名
*/
public static JSONArray forObjectToTreeMap(List<?> treeList, String parentId, String idKey, String parentIdKey, String childName) {
JSONArray childMenu = new JSONArray();
for (Object object : treeList) {
if (object != null) {
JSONObject jsonMenu = JSONObject.fromObject(object);
String menuId = jsonMenu.getString(idKey);
String pid = jsonMenu.getString(parentIdKey);
if (parentId.equals(pid)) {
JSONArray array = forObjectToTreeMap(treeList, menuId, idKey, parentIdKey, childName);
jsonMenu.put(childName, array);
childMenu.add(jsonMenu);
}
}
}
return childMenu;
}
######