Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当存在XML注释而不是元素时,Jackson XML反序列化失败_Java_Xml_Jackson_Jackson Dataformat Xml - Fatal编程技术网

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

Java 当存在XML注释而不是元素时,Jackson XML反序列化失败,java,xml,jackson,jackson-dataformat-xml,Java,Xml,Jackson,Jackson Dataformat Xml,我有以下XML: <licenseSummary> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9

我有以下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>
我尝试使用配置
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;
        }
}
公共类依赖项{
私有字符串groupId;
私有字符串工件;
私有字符串版本;
私有列表许可证=新的ArrayList(1);
公共字符串getGroupId(){
返回groupId;
}
public void setGroupId(字符串groupId){
this.groupId=groupId;
}
公共字符串getArtifactId(){
返回工件;
}
public void setArtifactId(字符串artifactId){
this.artifactId=artifactId;
}
公共字符串getVersion(){
返回版本;
}
公共void setVersion(字符串版本){
this.version=版本;
}
公共列表getLicenses(){
归还许可证;
}
公共许可证(列出许可证){
this.licenses=许可证;
}
}
有人知道我怎么可以忽略这个评论吗。(顺便说一句:我无法删除XML文件中的注释)


jackson dataformat xml
Version 2.9.4

我最终实现了一个
反序列化ProblemHandler
,并将其注册到
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);
                }
        }   
}
import java.io.IOException;
导入java.util.ArrayList;
导入com.fasterxml.jackson.core.JsonParser;
导入com.fasterxml.jackson.core.JsonToken;
导入com.fasterxml.jackson.databind.DeserializationContext;
导入com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
公共类LicenceDeserializationProblemHandler扩展了DeserializationProblemHandler{
@凌驾
公共对象handleUnexpectedToken(反序列化上下文ctxt、类targetType、JsonToken t、JsonParser p、,
字符串失败(MSG)引发IOException{
if(ArrayList.class.isAssignableFrom(targetType)&&
JsonToken.VALUE_STRING.equals(t)){
//如果没有许可证,则许可证令牌中会有XML注释。
返回null;
}否则{
返回super.handleUnexpectedToken(ctxt、targetType、t、p、failureMsg);
}
}   
}
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);
                }
        }   
}