有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当存在XMLComment而不是元素时,java Jackson XML反序列化失败

我有以下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文件时,Jackson失败,因为在第二个<dependency />-元素的<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


共 (1) 个答案

  1. # 1 楼答案

    我最终实现了一个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);
                    }
            }   
    }