- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Jackson 反序列化这种形式的 JSON:
{
"foo" : { "bar" : "baz" }
}
jackson 代码可能如下所示:
@JsonCreator
public class MyProxy {
@JsonProperty("foo") final FooProxy foo;
}
public class FooProxy {
@JsonProperty("bar") final String bar;
}
想象一下该 API 的使用者创建了如下所示的无效 JSON:
{
"foo" : { "bar" : 1 }
}
但是,在这种情况下,我收到 MismatchedInputException
并且错误如下所示:
Cannot construct instance of
MyProxy
(although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (1)
当我检查 MismatchedInputException 并调用 ex.getPathReference()
时,我得到:
FooProxy["bar"]->java.lang.Object[0]
我希望能够将损坏值的路径返回给用户,而无需引用底层 Java 类。
"foo.bar must be an Object."
如何返回带有 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;}
}
}
将会导致:
java.lang.RuntimeException: foo.bar must be of type Integer
关于java - 如何在 Jackson JsonMappingException 中获取 JSON 对象的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681300/
我以为我有一个父类参数,它有2个子类 ComboParameter 和 IntegerParameter @JsonSubTypes({ @JsonSubTypes.Type(value =
运行以下行时: Request> requestMap = JsonUtils.fromJson(eventContext.getMessage().getPayloadAsString(), Req
我已经使用 jersey 在 java 中编写了一个 REST API。当我使用 XML 请求时,一切正常,但当我使用 JSON 请求时,我总是得到 JsonMappingException。 这些对
我使用spring RestTemplate并将json响应转换为pojo。但得到一个 JsonMappingException POJO @Root public class Account { @
我的此类带有一个标有 @JsonCreator 的构造函数,用于将 JSON 字符串反序列化到我的类 Meal 中: public class Meal { private int prote
我的 IDE 在 mapper.readValue 行给我错误 Unhandled Exception com.fasterxml.jackson.databind.JsonMappingExcept
我在 Java 中有一个通用类型的类。它应该在收到一些 json 后返回一个 T 类型的对象。我正在使用以下代码创建对象: ObjectMapper mapper = new ObjectMapper
我有一个用 Java (spring boot) 编写的 rest api,我的请求从请求 header 中获取一个 json 字符串(不要问为什么这样:),例如{flowerId: 123}在我的
我正在尝试向我的网页显示数据库数据。当 GET 请求到 @RequestMapping(value = "/api/binder") 时,我做了以下代码。 但是当获取请求到达此方法时,它会获取数据(我
我正在构建一个使用并返回 JSON 的 RESTful Web 服务。当我尝试通过服务层从数据库中获取 ESRBRating 对象时,我遇到了以下堆栈跟踪。但是,当我将 Spring Data JPA
我在 Camel Route 中遇到了异常 Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer
对传递给 ObjectMapper Json 解析器方法的字符串进行 null 和空检查时,出现异常。这是我的代码: private static final ObjectMapper OBJECT_
我正在使用 Retrofit 2 和 jackson 转换器下载并解析此 json `{ "message": "Action complete.", "successCode": 0,
我知道关于我会告诉你的同一个问题有很多问题,但还没有一个能解决我的问题。 我已经实现了一项服务,该服务必须通过延迟加载来访问对象属性。这是类的简化: @Entity public class Trea
我有一个类需要使用 Jackson 从 JSON 反序列化。类结构如下所示: public class A { public B b; } public class B { publi
我尝试反序列化包含空属性并具有 JsonMappingException 的对象。 我的工作: String actual = "{\"@class\" : \"PersonResponse\","
尝试反序列化 JSON 时遇到以下异常 No suitable constructor found for type [simple type, class MyObj$obj$Card]: can
我有一个 Web 应用程序,其中服务器以 JSON 格式返回任何结果。为了创建 JSON,我使用 codehaus Jackson ObjectWriter 版本 1.9.8。 我遇到的问题是映射中有
我在尝试放置“可选实体过滤”时遇到问题。我有一个抽象类,如下所示: // In your Pom org.glassfish.jersey.ext je
尝试使用 @JsonIdentityInfo jackson 注释时出错。当我尝试反序列化对象时,出现以下异常: Could not read JSON: Already had POJO for i
我是一名优秀的程序员,十分优秀!