Several highly reusable classes in Java Web development

Source: Internet
Author: User
Tags parse string java web

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.