jackson反序列化嵌套对象HttpMessageNotReadableException:“无法读取JSON:无法识别的字段......”

时间:2016-05-10 08:11:29

标签: java json spring-mvc jackson

我有一个这样的模型:

...

private TestClass test;

public TestClass getTest(){
    return test;
}

@JsonDeserialize(using = JsonDeserializerTest.class)
public void setTest(TestClass test){
    this.test = test;
}

这是我的TestClass

public class TestClass{

      private Integer field1;
      private Integer field2;

       public Integer getField1() {
           return field1;
       }

       public void setField1(Integer field1) {
          this.field1 = field1;
       }

       public Integer getField2() {
          return field2;
       }

       public void setField2(Integer field2) {
          this.field2 = field2;
       }
}

最后这里是JsonDeserializerTest

@Component
public class JsonDeserializerTest extends JsonDeserializer<TestClass> {


    @Override
    public TestClass deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
        TestClass test = new TestClass();
        //do some login
        return test;
    }

}

当我提交表单并将test内部模型发布到控制器时,调用了deserialize方法,但它给了我以下异常:

Could not read JSON: Unrecognized field "field1" (class ***.***.MyModel), not marked as ignorable

我该如何解决这个问题?

此外,我在模型中@JsonProperty之上使用了TestClass,但问题未解决。

0 个答案:

没有答案