1 回答
TA贡献1806条经验 获得超8个赞
添加一个扩展的类,如果为 null,则返回 null:JsonDeserializer<JsonNode>parser.getText()
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
public class JsonNodeDeserializer extends JsonDeserializer<JsonNode> {
@Override
public JsonNode deserialize(JsonParser parser, DeserializationContext context)
throws IOException {
String value = parser.getText();
if(value == null) {
return null;
}
return (JsonNode) context.findRootValueDeserializer(context.constructType(JsonNode.class)).deserialize(parser, context);
}
}
然后对属性进行注释,以告诉 Jackson 使用您的自定义反序列化程序:jsonNode@JsonDeserialize(using = JsonNodeDeserializer.class)
@Data
@Accessors(chain = true)
@JsonInclude(Include.NON_NULL)
public class TestPOJO {
private Long id;
private String name;
@JsonDeserialize(using = JsonNodeDeserializer.class)
private JsonNode jsonNode;
// getters and setters
}
添加回答
举报