【问题标题】:ObjectNode returned with unknown valuesObjectNode 返回未知值
【发布时间】:2018-06-07 06:07:10
【问题描述】:

我在我的 java-rest 中使用 com.fasterxml.jackson.databind.node.ObjectNode 来返回一个简单的值:

@POST
@Path ("/JsonApi")
@Consumes("application/json")
@Produces("application/json")
public ObjectNode postEmployee(JSONObject jsonObject)throws Exception{
    String onlineUrl = MatafConfig.getConfigParam(request,
            m_CONFIG_REDIRECT);
    if(m_mapper == null)
        m_mapper = new ObjectMapper();
    ObjectNode jNode = m_mapper.createObjectNode();
    jNode.put("url", onlineUrl); // debug shows {"url": "/mySite"}

    //return url


    return jNode;
}

jNode 中的值与预期的 {"url": "/mySite"} 一致,但在客户端显示其他 json -

{"nodeType":"OBJECT","valueNode":false,"containerNode":true,"missingNode":false,"array":false,"object":true,"pojo":false,"number":false,"integralNumber":false,"floatingPointNumber":false,"short":false,"int":false,"long":false,"float":false,"double":false,"bigDecimal":false,"bigInteger":false,"textual":false,"boolean":false,"null":false,"binary":false}

我不知道这个 json,我认为它可能是来自 jax-rs 的一些默认对象。

更多细节:java 8,jackson 2.6,使用 WebSphere 8.5 默认jax-rs libs。

【问题讨论】:

    标签: java json jax-rs


    【解决方案1】:

    似乎当您将 ObjectNode 作为返回参数传递时,Jackson 反序列化 ObjectNode 而不是它的内容。 因此,不要返回 ObjectNode,而是尝试创建类,例如“Url”并返回它。 这是该课程的示例:

    public class Url {
    
       private String url;
    
       public Url(final String url) {
           this.url = url;
       }
    
       public String getUrl() {
           return url;
       }
    }
    

    当你返回它时,它会自动转换为 JSON 表示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多