##背景 一個費用的類,裏面有各類費用,測試的時候數據不完整致使不少字段都是null。開始的時候返回給客戶端的字段自動把null去掉了,但這樣致使客戶端接收的model不完整,本身添加字段的邏輯有點複雜。所以仍是贊成服務器給null設置默認值。html
string要返回空字符串而不是null。由於使用springmvc,默認使用jackson來作json轉化。那麼能夠採用自定義jackson的方式來設置null爲"".java
/** * Copyright © 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. */ import java.io.IOException; import java.util.List; import java.util.Map; import java.util.TimeZone; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonParser.Feature; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.util.JSONPObject; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; import com.google.common.collect.Lists; import com.google.common.collect.Maps; /** * 簡單封裝Jackson,實現JSON String<->Java Object的Mapper. * 封裝不一樣的輸出風格, 使用不一樣的builder函數建立實例. * @author ThinkGem * @version 2013-11-15 */ public class JsonMapper extends ObjectMapper { private static final long serialVersionUID = 1L; private static Logger logger = LoggerFactory.getLogger(JsonMapper.class); private static JsonMapper mapper; public JsonMapper() { this(Include.NON_EMPTY); } public JsonMapper(Include include) { // 設置輸出時包含屬性的風格 if (include != null) { this.setSerializationInclusion(include); } // 容許單引號、容許不帶引號的字段名稱 this.enableSimple(); // 設置輸入時忽略在JSON字符串中存在但Java對象實際沒有的屬性 this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // 空值處理爲空串 this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>(){ @Override public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(""); } }); // 進行HTML解碼。 this.registerModule(new SimpleModule().addSerializer(String.class, new JsonSerializer<String>(){ @Override public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(StringEscapeUtils.unescapeHtml4(value)); } })); // 設置時區 this.setTimeZone(TimeZone.getDefault());//getTimeZone("GMT+8:00") } /** * 建立只輸出非Null且非Empty(如List.isEmpty)的屬性到Json字符串的Mapper,建議在外部接口中使用. */ public static JsonMapper getInstance() { if (mapper == null){ mapper = new JsonMapper().enableSimple(); } return mapper; } /** * 建立只輸出初始值被改變的屬性到Json字符串的Mapper, 最節約的存儲方式,建議在內部接口中使用。 */ public static JsonMapper nonDefaultMapper() { if (mapper == null){ mapper = new JsonMapper(Include.NON_DEFAULT); } return mapper; } /** * Object能夠是POJO,也能夠是Collection或數組。 * 若是對象爲Null, 返回"null". * 若是集合爲空集合, 返回"[]". */ public String toJson(Object object) { try { return this.writeValueAsString(object); } catch (IOException e) { logger.warn("write to json string error:" + object, e); return null; } } /** * 反序列化POJO或簡單Collection如List<String>. * * 若是JSON字符串爲Null或"null"字符串, 返回Null. * 若是JSON字符串爲"[]", 返回空集合. * * 如需反序列化複雜Collection如List<MyBean>, 請使用fromJson(String,JavaType) * @see #fromJson(String, JavaType) */ public <T> T fromJson(String jsonString, Class<T> clazz) { if (StringUtils.isEmpty(jsonString)) { return null; } try { return this.readValue(jsonString, clazz); } catch (IOException e) { logger.warn("parse json string error:" + jsonString, e); return null; } } /** * 反序列化複雜Collection如List<Bean>, 先使用函數createCollectionType構造類型,而後調用本函數. * @see #createCollectionType(Class, Class...) */ @SuppressWarnings("unchecked") public <T> T fromJson(String jsonString, JavaType javaType) { if (StringUtils.isEmpty(jsonString)) { return null; } try { return (T) this.readValue(jsonString, javaType); } catch (IOException e) { logger.warn("parse json string error:" + jsonString, e); return null; } } /** * 構造泛型的Collection Type如: * ArrayList<MyBean>, 則調用constructCollectionType(ArrayList.class,MyBean.class) * HashMap<String,MyBean>, 則調用(HashMap.class,String.class, MyBean.class) */ public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { return this.getTypeFactory().constructParametricType(collectionClass, elementClasses); } /** * 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性. */ @SuppressWarnings("unchecked") public <T> T update(String jsonString, T object) { try { return (T) this.readerForUpdating(object).readValue(jsonString); } catch (JsonProcessingException e) { logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e); } catch (IOException e) { logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e); } return null; } /** * 輸出JSONP格式數據. */ public String toJsonP(String functionName, Object object) { return toJson(new JSONPObject(functionName, object)); } /** * 設定是否使用Enum的toString函數來讀寫Enum, * 為False時時使用Enum的name()函數來讀寫Enum, 默認為False. * 注意本函數必定要在Mapper創建後, 全部的讀寫動做以前調用. */ public JsonMapper enableEnumUseToString() { this.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); this.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); return this; } /** * 支持使用Jaxb的Annotation,使得POJO上的annotation不用與Jackson耦合。 * 默認會先查找jaxb的annotation,若是找不到再找jackson的。 */ public JsonMapper enableJaxbAnnotation() { JaxbAnnotationModule module = new JaxbAnnotationModule(); this.registerModule(module); return this; } /** * 容許單引號 * 容許不帶引號的字段名稱 */ public JsonMapper enableSimple() { this.configure(Feature.ALLOW_SINGLE_QUOTES, true); this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); return this; } /** * 取出Mapper作進一步的設置或使用其餘序列化API. */ public ObjectMapper getMapper() { return this; } /** * 對象轉換爲JSON字符串 * @param object * @return */ public static String toJsonString(Object object){ return JsonMapper.getInstance().toJson(object); } /** * JSON字符串轉換爲對象 * @param jsonString * @param clazz * @return */ public static Object fromJsonString(String jsonString, Class<?> clazz){ return JsonMapper.getInstance().fromJson(jsonString, clazz); } /** * 測試 */ public static void main(String[] args) { List<Map<String, Object>> list = Lists.newArrayList(); Map<String, Object> map = Maps.newHashMap(); map.put("id", 1); map.put("pId", -1); map.put("name", "根節點"); list.add(map); map = Maps.newHashMap(); map.put("id", 2); map.put("pId", 1); map.put("name", "你好"); map.put("open", true); list.add(map); String json = JsonMapper.getInstance().toJson(list); System.out.println(json); } }
除了String之外的類型就很差統一設定了,畢竟各自的format不一樣。因此採用數據傳輸對象(DTO)(Data Transfer Object)
來包裝響應。那麼,在包裝的時候處理下null就行了。git
當前的問題是,一個費用的類型的字段太多,好比double的費用由十幾個,那麼if-else會寫吐的,所以採用反射來設置double的默認爲0。github
Class<FeeVo> clazz = FeeVo.class; Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { Class<?> type = field.getType(); String name = field.getName(); if (Double.class.equals(type)){//double類型設置默認爲0 try { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, clazz); Method readMethod = propertyDescriptor.getReadMethod(); Object value = readMethod.invoke(this); if (value==null){ Reflections.setFieldValue(this, name,0d); LOGGER.debug("設置double字段[{}]爲0",name); } } catch (IntrospectionException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }