public static < T > String toJson ( T obj ) { StringWriter writer = new StringWriter ( ) ; String jsonStr = "" ; JsonGenerator gen = null ; try { gen = getJsonFactory ( ) . createGenerator ( writer ) ; getMapper ( ) . writeValue ( gen , obj ) ; writer . flush ( ) ; jsonStr = writer . toString ( ) ; } catch ( IOException e ) { log . error ( "{}" , e . getMessage ( ) , e ) ; } finally { if ( gen != null ) { try { gen . close ( ) ; } catch ( IOException e ) { } } } return jsonStr ; }
Bean to json string