Вы можете зарегистрировать свой собственный DeserializationProblemHandler
и вернуть null
, когда появится <value xsi:nil="true" />
:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class XmlMapperApp {
public static void main(String[] args) throws Exception {
File xmlFile = new File("./resource/test.xml").getAbsoluteFile();
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setConfig(xmlMapper.getDeserializationConfig().withHandler(new NullableItemsDeserializationProblemHandler()));
System.out.println(xmlMapper.readValue(xmlFile, Pojo.class));
}
}
class NullableItemsDeserializationProblemHandler extends DeserializationProblemHandler {
@Override
public Object handleUnexpectedToken(DeserializationContext ctxt, Class<?> targetType, JsonToken t, JsonParser p, String failureMsg) throws IOException {
if (targetType == Long.class && p.currentToken() == JsonToken.START_OBJECT) {
boolean isNull = false;
while (p.currentToken() != JsonToken.END_OBJECT) {
p.nextToken();
switch (p.currentToken()) {
case FIELD_NAME:
if ("nil".equals(p.getText())) {
isNull = true;
}
}
}
if (isNull) {
return null;
}
}
return super.handleUnexpectedToken(ctxt, targetType, t, p, failureMsg);
}
}
Над кодом напечатано:
Pojo{values=[10, null, 20]}
См. Также: