我正在使用 Jackson 反序列化这种形式的 JSON:
{
"foo" : { "bar" : "baz" }
}
Run Code Online (Sandbox Code Playgroud)
杰克逊代码可能如下所示:
@JsonCreator
public class MyProxy {
@JsonProperty("foo") final FooProxy foo;
}
public class FooProxy {
@JsonProperty("bar") final String bar;
}
Run Code Online (Sandbox Code Playgroud)
想象一下这个 API 的使用者创建了如下无效的 JSON:
{
"foo" : { "bar" : 1 }
}
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,我收到一个MismatchedInputException错误,如下所示:
无法构造实例
MyProxy(尽管至少存在一个 Creator):没有 int/Int 参数构造函数/工厂方法来从 Number 值反序列化 (1)
当我检查 MismatchedInputException 并调用时,我ex.getPathReference()得到:
FooProxy["bar"]->java.lang.Object[0]
我希望能够将损坏值的路径返回给用户,而无需引用底层 Java 类。
"foo.bar must be an Object."
Run Code Online (Sandbox Code Playgroud)
如何返回带有 JSON 路径的错误消息,并删除对 Java 实现的任何引用?
像这样的事情应该这样做:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
public class DeserializationErrorTest {
@Test
void testDeserializeWrongInput() throws IOException {
final ObjectMapper mapper = new ObjectMapper();
try {
mapper.readValue("{\"foo\" : { \"bar\" : \"not-int\" }}", MyProxy.class);
} catch (MismatchedInputException e) {
throw remapMismatchedInputException(e, RuntimeException.class);
}
}
private <T extends Exception> T remapMismatchedInputException(final MismatchedInputException e, Class<T> exClass) {
try {
final String fieldName =
e.getPath().stream().map(JsonMappingException.Reference::getFieldName).collect(Collectors.joining("."));
return exClass.getConstructor(String.class).newInstance(fieldName + " must be of type " + e.getTargetType().getSimpleName());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException pE) {
throw new IllegalArgumentException("Cannot instantiate exception class " + exClass.getSimpleName());
}
}
static class MyProxy {
@JsonProperty("foo") final FooProxy foo;
@JsonCreator
public MyProxy(@JsonProperty("foo") final FooProxy pFoo) {foo = pFoo;}
}
static class FooProxy {
@JsonProperty("bar") final Integer bar;
@JsonCreator
public FooProxy(@JsonProperty("bar") final Integer pBar) {bar = pBar;}
}
}
Run Code Online (Sandbox Code Playgroud)
并将导致:
java.lang.RuntimeException: foo.bar must be of type Integer
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2328 次 |
| 最近记录: |