我有一个这样的模型:
...
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
,但问题未解决。