【fix sonar】JacksonUtil.java文件
This commit is contained in:
+10
-22
@@ -1,9 +1,6 @@
|
|||||||
package com.xxl.job.admin.core.util;
|
package com.xxl.job.admin.core.util;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -12,23 +9,26 @@ import java.io.IOException;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Jackson util
|
* Jackson util
|
||||||
*
|
*
|
||||||
* 1、obj need private and set/get;
|
* 1、obj need private and set/get;
|
||||||
* 2、do not support inner class;
|
* 2、do not support inner class;
|
||||||
*
|
*
|
||||||
* @author xuxueli 2015-9-25 18:02:56
|
* @author xuxueli 2015-9-25 18:02:56
|
||||||
*/
|
*/
|
||||||
public class JacksonUtil {
|
public class JacksonUtil {
|
||||||
|
private JacksonUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
|
private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
|
||||||
|
|
||||||
private final static ObjectMapper objectMapper = new ObjectMapper();
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
public static ObjectMapper getInstance() {
|
public static ObjectMapper getInstance() {
|
||||||
return objectMapper;
|
return objectMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* bean、array、List、Map --> json
|
* bean、array、List、Map --> json
|
||||||
*
|
*
|
||||||
* @param obj
|
* @param obj
|
||||||
* @return json string
|
* @return json string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -36,10 +36,6 @@ public class JacksonUtil {
|
|||||||
public static String writeValueAsString(Object obj) {
|
public static String writeValueAsString(Object obj) {
|
||||||
try {
|
try {
|
||||||
return getInstance().writeValueAsString(obj);
|
return getInstance().writeValueAsString(obj);
|
||||||
} catch (JsonGenerationException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} catch (JsonMappingException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@@ -48,7 +44,7 @@ public class JacksonUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* string --> bean、Map、List(array)
|
* string --> bean、Map、List(array)
|
||||||
*
|
*
|
||||||
* @param jsonStr
|
* @param jsonStr
|
||||||
* @param clazz
|
* @param clazz
|
||||||
* @return obj
|
* @return obj
|
||||||
@@ -57,11 +53,7 @@ public class JacksonUtil {
|
|||||||
public static <T> T readValue(String jsonStr, Class<T> clazz) {
|
public static <T> T readValue(String jsonStr, Class<T> clazz) {
|
||||||
try {
|
try {
|
||||||
return getInstance().readValue(jsonStr, clazz);
|
return getInstance().readValue(jsonStr, clazz);
|
||||||
} catch (JsonParseException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} catch (JsonMappingException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -80,11 +72,7 @@ public class JacksonUtil {
|
|||||||
try {
|
try {
|
||||||
JavaType javaType = getInstance().getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
JavaType javaType = getInstance().getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
||||||
return getInstance().readValue(jsonStr, javaType);
|
return getInstance().readValue(jsonStr, javaType);
|
||||||
} catch (JsonParseException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} catch (JsonMappingException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user