【问题标题】:Issue while Sending JSON Request发送 JSON 请求时出现问题
【发布时间】:2015-07-29 17:35:40
【问题描述】:

我在发送 JSON 格式的 REST 请求时遇到问题。调用服务时丢失了一些参数。但是当我以 xml 格式发送请求时它工作正常。我的问题得到以下错误:

SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "create_session_param"

对象映射器类如下所示:

objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);

        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,true);

        JaxbAnnotationModule module = new JaxbAnnotationModule();
        // maintain JAXB annotation support
        objectMapper.registerModule(module);

有人可以帮我解决这个问题吗?

谢谢。

【问题讨论】:

  • 您能否提供您尝试序列化的类(无论如何是相关部分)以及您尝试解析的 JSON 示例?
  • @AlexTaylor 我已经更新了问题。你能帮忙吗。

标签: json rest jersey jackson jax-rs


【解决方案1】:

您只有 WRAP_ROOT_VALUE 用于序列化。记住 序列化 是 POJO 到 JSON。 SerializationFeature.WRAP_ROOT_VALUE 是在创建 JSON 时实际添加 "create_session_param" 的那个。

我们需要JSON to POJO,也就是反序列化,它有自己的特性集。在这种情况下,我们需要一个特性来un将根值包装在 JSON 中。为此有

DeserializationFeature.UNWRAP_ROOT_VALUE

也是这样

mapper.configure(DeserializationFeature UNWRAP_ROOT_VALUE, true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    相关资源
    最近更新 更多