存在param时从json转换为object时出错

时间:2015-10-05 13:26:45

标签: java jackson

我正在尝试使用Jackson lib将Json转换为JsonParams对象,但是当属性UnrecognizedPropertyException存在时,我会收到client_id的错误。这是错误[1],我用来测试[2]的类,以及JsonParams类[3]。为什么我会收到此错误?

[1]错误输出

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "client_id" (Class org.apache.hadoop.mapred.JsonParams), not marked as ignorable
at [Source: java.io.StringReader@1eee4b8; line: 1, column: 81] (through reference chain: org.apache.hadoop.mapred.JsonParams["client_id"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at org.apache.hadoop.mapred.Medusa.init(Medusa.java:67)
at org.apache.hadoop.mapred.Medusa.submit(Medusa.java:355)
at org.apache.hadoop.mapred.Medusa.runJob(Medusa.java:331)
at org.apache.hadoop.mapred.examples.MyWordCount.main(MyWordCount.java:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.hadoop.util.ProgramDriver$ProgramDescription.invoke(ProgramDriver.java:71)
at org.apache.hadoop.util.ProgramDriver.run(ProgramDriver.java:144)
at org.apache.hadoop.util.ProgramDriver.driver(ProgramDriver.java:152)
at org.apache.hadoop.mapred.examples.ExampleDriver.main(ExampleDriver.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
java.lang.NullPointerException

[2]要测试的课程

package org.apache.hadoop.mapred.examples;
import org.apache.hadoop.mapred.JsonParams;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;

public class JsonToClassTest {
  public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    String content = "{\"output_path\": \"/output1-1443369483\", \"input_path\": [\"/input1\"], \"client_id\": \"1443369483\", \"type\": \"-map\", \"should_tamper_digest\":\"true\"}";
    try {
        Object params =  mapper.readValue(content, JsonParams.class);
        System.out.println(params.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

[3] Json params class

package org.apache.hadoop.mapred;

public class JsonParams {
  private String type;
  private String client_id;
  private String output_path;
  private String[] input_path;
  private String should_tamper_digest;

  public String[] getInput_path() {
    return input_path;
  }

  public String getType() {
    return type;
  }

  public String getOutput_path() {
    return output_path;
  }

  public String getShouldTamperDigest() {
    return should_tamper_digest;
  }

  @Override
  public String toString() {
    return "JsonParams [type=" + type + ", client_id=" + client_id+ ", " +
            "output_path=" + output_path + ", should_tamper_digest=" + should_tamper_digest + "]";
  }
}

1 个答案:

答案 0 :(得分:1)

向您JsonParams类添加setter。它应该给o / p

JsonParams [type=-map, client_id=1443369483, output_path=/output1-1443369483, should_tamper_digest=true]