Jackson XML 当存在 XML-注释而不是元素时反序列化失败

Jackson XML Deserialiation fails when there is a XML-Comment instead of elements

我有以下 XML:

<licenseSummary>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
            <licenses>
                <license>
                   <name>The Apache Software License, Version 2.0</name>
                   <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
                   <distribution>repo</distribution>
                </license>
            </licenses>
        </dependency>
        <dependency>
            <groupId>somegroup</groupId>
            <artifactId>someartifact</artifactId>
            <version>2.43.0-SNAPSHOT</version>
            <licenses>
                <!--No license information available. -->
            </licenses>
        </dependency>
    </dependencies>
</licenseSummary>

尝试反序列化此 XML-File Jackson 失败,因为在第二个 <dependency />-Element 的 <licenses /> 部分中有注释而不是元素,并出现以下错误:

Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token

我尝试配置 XMLMapper 但这没有帮助:

ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

相关的 POJO 是这样的:

public class Dependency {
        private String groupId;
        private String artifactId;
        private String version;
        private List<License> licenses = new ArrayList<License>(1);

        public String getGroupId() {
                return groupId;
        }
        public void setGroupId(String groupId) {
                this.groupId = groupId;
        }
        public String getArtifactId() {
                return artifactId;
        }
        public void setArtifactId(String artifactId) {
                this.artifactId = artifactId;
        }
        public String getVersion() {
                return version;
        }
        public void setVersion(String version) {
                this.version = version;
        }
        public List<License> getLicenses() {
                return licenses;
        }
        public void setLicenses(List<License> licenses) {
                this.licenses = licenses;
        }
}

有谁知道我怎样才能忽略评论。 (顺便说一句:我无法删除 XML 文件中的评论)

jackson-dataformat-xml 版本 2.9.4

我最终实现了一个 DeserializationProblemHandler 并将其注册到 xmlMapper,如下所示:

xmlMapper.addHandler(new LicenceDeserializationProblemHandler());

这是处理程序:

import java.io.IOException;
import java.util.ArrayList;

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;

public class LicenceDeserializationProblemHandler extends DeserializationProblemHandler {

        @Override
        public Object handleUnexpectedToken(DeserializationContext ctxt, Class<?> targetType, JsonToken t, JsonParser p,
                        String failureMsg) throws IOException {
                if(ArrayList.class.isAssignableFrom(targetType) && 
                                JsonToken.VALUE_STRING.equals(t)) {
                        // If there is no license a XML-Comment is in the licenses token.
                        return null;
                }else {
                        return super.handleUnexpectedToken(ctxt, targetType, t, p, failureMsg);
                }
        }   
}