我正在反序列化JSON http响应并将其读入Java对象。
我的第一个解决方案是:
javax.ws.rs.core.Response response = ...
MyClass obj = response.readEntity(MyClass.class);
我想将MyClass中枚举成员的未知枚举值读为null并且不抛出异常,因此我将上述内容修改为:
String str = response.readEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
MyClass obj = mapper.readValue(string, Class<MyClass>);
有谁知道我怎么能避免这里的双重反序列化,而不是响应 - &gt;字符串 - &gt;类,只做响应 - &gt;类?
答案 0 :(得分:0)
因此解决方案是向客户端
注册ObjectMapperContextResolverprivate final ObjectMapperContextResolver OBJECT_MAPPER_CONTEXT_RESOLVER = new ObjectMapperContextResolver();
javax.ws.rs.client.Client client = ...
javax.ws.rs.client.WebTarget target = client.target("...");
client.register(OBJECT_MAPPER_CONTEXT_RESOLVER).target("...");
javax.ws.rs.core.Response response = target.path("...").request(MediaType.APPLICATION_JSON).get();
MyClass obj = response.readEntity(MyClass.class);