Handle serialization exception in JacksonUtils

This commit is contained in:
KomachiSion 2020-05-26 11:51:47 +08:00
parent b443279ed0
commit ff6e09e58d
4 changed files with 147 additions and 23 deletions

View File

@ -0,0 +1,50 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.api.exception;
/**
* Nacos deserialization exception.
*
* @author yangyi
*/
public class NacosDeserializationException extends RuntimeException {
private static final long serialVersionUID = -2742350751684273728L;
private static final String DEFAULT_MSG = "Nacos deserialize failed. ";
private static final String MSG_FOR_SPECIFIED_CLASS = "Nacos deserialize for class [%s] failed. ";
private Class<?> targetClass;
public NacosDeserializationException() {
}
public NacosDeserializationException(Class<?> targetClass) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()));
this.targetClass = targetClass;
}
public NacosDeserializationException(Throwable throwable) {
super(DEFAULT_MSG, throwable);
}
public NacosDeserializationException(Class<?> targetClass, Throwable throwable) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()), throwable);
this.targetClass = targetClass;
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.api.exception;
/**
* Nacos serialization exception.
*
* @author yangyi
*/
public class NacosSerializationException extends RuntimeException {
private static final long serialVersionUID = -4308536346316915612L;
private static final String DEFAULT_MSG = "Nacos serialize failed. ";
private static final String MSG_FOR_SPECIFIED_CLASS = "Nacos serialize for class [%s] failed. ";
private Class<?> serializedClass;
public NacosSerializationException() {
}
public NacosSerializationException(Class<?> serializedClass) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName()));
this.serializedClass = serializedClass;
}
public NacosSerializationException(Throwable throwable) {
super(DEFAULT_MSG, throwable);
}
public NacosSerializationException(Class<?> serializedClass, Throwable throwable) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName()), throwable);
this.serializedClass = serializedClass;
}
}

View File

@ -16,10 +16,14 @@
package com.alibaba.nacos.common.utils;
import com.alibaba.nacos.api.exception.NacosDeserializationException;
import com.alibaba.nacos.api.exception.NacosSerializationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import java.io.IOException;
import java.lang.reflect.Type;
/**
@ -33,29 +37,53 @@ public final class JacksonUtils {
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
public static String toJson(Object obj) throws Exception {
return mapper.writeValueAsString(obj);
}
public static String toJson(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new NacosSerializationException(obj.getClass());
}
}
public static byte[] toJsonBytes(Object obj) throws Exception {
return ByteUtils.toBytes(mapper.writeValueAsString(obj));
}
public static byte[] toJsonBytes(Object obj) {
try {
return ByteUtils.toBytes(mapper.writeValueAsString(obj));
} catch (JsonProcessingException e) {
throw new NacosSerializationException(obj.getClass());
}
}
public static <T> T toObj(byte[] json, Class<T> cls) throws Exception {
return toObj(StringUtils.newString4UTF8(json), cls);
}
public static <T> T toObj(byte[] json, Class<T> cls) {
try {
return toObj(StringUtils.newString4UTF8(json), cls);
} catch (Exception e) {
throw new NacosDeserializationException(cls);
}
}
public static <T> T toObj(byte[] json, Type cls) throws Exception {
return toObj(StringUtils.newString4UTF8(json), cls);
}
public static <T> T toObj(byte[] json, Type cls) {
try {
return toObj(StringUtils.newString4UTF8(json), cls);
} catch (Exception e) {
throw new NacosDeserializationException();
}
}
public static <T> T toObj(String json, Class<T> cls) throws Exception {
return mapper.readValue(json, cls);
}
public static <T> T toObj(String json, Class<T> cls) {
try {
return mapper.readValue(json, cls);
} catch (IOException e) {
throw new NacosDeserializationException(cls);
}
}
public static <T> T toObj(String json, Type type) throws Exception {
return mapper.readValue(json, mapper.constructType(type));
}
public static <T> T toObj(String json, Type type) {
try {
return mapper.readValue(json, mapper.constructType(type));
} catch (IOException e) {
throw new NacosDeserializationException();
}
}
public static void registerSubtype(Class<?> clz, String type) {
mapper.registerSubtypes(new NamedType(clz, type));

View File

@ -177,11 +177,7 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
}
public String toJSON() {
try {
return JacksonUtils.toJson(this);
} catch (Exception e) {
throw new RuntimeException("Instance toJSON failed", e);
}
return JacksonUtils.toJson(this);
}