Commit 5939a3d5 by Nepxion

重构类结构

parent fb5f4557
......@@ -16,6 +16,8 @@ import org.apache.commons.io.IOUtils;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
public class ConsoleErrorHandler extends DefaultResponseErrorHandler {
private String cause;
......@@ -23,7 +25,7 @@ public class ConsoleErrorHandler extends DefaultResponseErrorHandler {
public void handleError(ClientHttpResponse response) throws IOException {
// 这里绝对不能关闭InputStream
InputStream inputStream = response.getBody();
cause = IOUtils.toString(inputStream, "UTF-8");
cause = IOUtils.toString(inputStream, DiscoveryConstant.ENCODING_UTF_8);
}
public String getCause() {
......
......@@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory;
import com.nepxion.discovery.common.entity.RuleEntity;
import com.nepxion.discovery.common.exception.DiscoveryException;
import com.nepxion.discovery.plugin.configcenter.parser.json.jackson.JacksonSerializer;
import com.nepxion.discovery.common.util.JsonUtil;
import com.nepxion.discovery.plugin.framework.config.PluginConfigParser;
public class JsonConfigParser implements PluginConfigParser {
......@@ -28,7 +28,7 @@ public class JsonConfigParser implements PluginConfigParser {
}
try {
RuleEntity ruleEntity = JacksonSerializer.fromJson(config, RuleEntity.class);
RuleEntity ruleEntity = JsonUtil.fromJson(config, RuleEntity.class);
ruleEntity.setContent(config);
LOG.info("Rule entity=\n{}", ruleEntity);
......
package com.nepxion.discovery.plugin.configcenter.parser.json.jackson;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
public class JacksonSerializer {
private static ObjectMapper objectMapper;
static {
objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat(DiscoveryConstant.DATE_FORMAT));
}
public static <T> String toJson(T object) {
if (object == null) {
throw new IllegalArgumentException("Object is null");
}
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
public static <T> T fromJson(String json, Class<T> clazz) {
if (StringUtils.isEmpty(json)) {
throw new IllegalArgumentException("Json is null or empty");
}
try {
return objectMapper.readValue(json, clazz);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
public static <T> T fromJson(String json, TypeReference<T> typeReference) {
if (StringUtils.isEmpty(json)) {
throw new IllegalArgumentException("Json is null or empty");
}
try {
return objectMapper.readValue(json, typeReference);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
public static ObjectMapper getObjectMapper() {
return objectMapper;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment