1. Jsonutil
Implement a string-to-object, object-to-string class, use scenario, Redis for Single sign-on using the methods provided by the Jackson package
PackageCom.mmall.util;Importcom.google.common.collect.Lists;Importcom.mmall.pojo.Category;ImportCom.mmall.pojo.TestPojo;ImportCom.mmall.pojo.User;Importlombok.extern.slf4j.Slf4j;Importorg.apache.commons.lang.StringUtils;ImportOrg.codehaus.jackson.map.DeserializationConfig;ImportOrg.codehaus.jackson.map.ObjectMapper;ImportOrg.codehaus.jackson.map.SerializationConfig;Importorg.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;ImportOrg.codehaus.jackson.type.JavaType;Importorg.codehaus.jackson.type.TypeReference;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;/*** Created by Geely*/@Slf4j Public classJsonutil {Private StaticObjectmapper Objectmapper =NewObjectmapper (); Static{ //all fields of the object are listed in theobjectmapper.setserializationinclusion (inclusion.always); //cancels the default conversion timestamps formObjectmapper.configure (SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false); //error ignoring empty bean to JSONObjectmapper.configure (SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,false); //All date formats are unified into the following styles, i.e. Yyyy-mm-dd HH:mm:ssObjectmapper.setdateformat (NewSimpleDateFormat (Datetimeutil.standard_format)); //ignoring the existence of a JSON string, but there is no corresponding attribute in the Java object. Prevent ErrorsObjectmapper.configure (DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false); } Public Static<T>String obj2string (T obj) {if(obj = =NULL){ return NULL; } Try { returnObjinstanceofString?(String) obj:objectMapper.writeValueAsString (obj); } Catch(Exception e) {Log.warn ("Parse Object to String error", E); return NULL; } } Public Static<T>String obj2stringpretty (T obj) {if(obj = =NULL){ return NULL; } Try { returnObjinstanceofString?(String) Obj:objectMapper.writerWithDefaultPrettyPrinter (). writevalueasstring (obj); } Catch(Exception e) {Log.warn ("Parse Object to String error", E); return NULL; } } Public Static<T> T string2obj (String str,class<t>clazz) { if(Stringutils.isempty (str) | | clazz = =NULL){ return NULL; } Try { returnClazz.equals (String.class)?(T) str:objectMapper.readValue (Str,clazz); } Catch(Exception e) {Log.warn ("Parse String to Object error", E); return NULL; } } Public Static<T> T string2obj (String str, typereference<t>typereference) { if(Stringutils.isempty (str) | | typereference = =NULL){ return NULL; } Try { returnT (Typereference.gettype (). Equals (String.class)?Str:objectMapper.readValue (str,typereference)); } Catch(Exception e) {Log.warn ("Parse String to Object error", E); return NULL; } } Public Static<T> T string2obj (String str,class<?> collectionclass,class<?>... elementclasses) {Javatype Javatype=objectmapper.gettypefactory (). Constructparametrictype (collectionclass,elementclasses); Try { returnObjectmapper.readvalue (Str,javatype); } Catch(Exception e) {Log.warn ("Parse String to Object error", E); return NULL; } }}
Several highly reusable classes in Java Web development