Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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 使用Jackson从JAXB生成的XML反序列化映射_Java_Xml_Jackson_Jaxb - Fatal编程技术网

Java 使用Jackson从JAXB生成的XML反序列化映射

Java 使用Jackson从JAXB生成的XML反序列化映射,java,xml,jackson,jaxb,Java,Xml,Jackson,Jaxb,我需要反序列化使用JAXB生成的一些XML。 由于一些法规遵从性问题,我只能使用Jackson进行XML解析。 在尝试反序列化具有映射的类时,我遇到以下异常 com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token at [Source: (StringReader); line:

我需要反序列化使用JAXB生成的一些XML。 由于一些法规遵从性问题,我只能使用Jackson进行XML解析。 在尝试反序列化具有映射的类时,我遇到以下异常

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
 at [Source: (StringReader); line: 40, column: 21] (through reference chain: FileConfig["otherConfigs"]->java.util.LinkedHashMap["entry"])
我的代码如下:

XML文件:

。 .


假的
……某个地点。。。。
app.log
…一些绳子。。。
路径类型
1.
。 .

FileConfig.java

    public class FileConfig implements Serializable {

    protected Boolean whetherNotify = false;
    protected String url;
    protected String includes;
    protected FileType fileType;
    private Map<String, String> otherConfigs = new HashMap<String, String>();

    ....getters and setters.....
}
public类FileConfig实现可序列化{
受保护布尔值whetherNotify=false;
受保护的字符串url;
受保护字符串包括:;
受保护的文件类型文件类型;
私有映射otherConfigs=新HashMap();
…接受者和接受者。。。。。
}
Main.java

public class Main {
  .
  .
  .
  .
   private static <T> T unmarshallFromXML(String xml, Class<T> parseToClass) throws IOException {
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        xmlMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
        xmlMapper.setDefaultSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        xmlMapper.setDefaultUseWrapper(false);
        xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        T parsedObject = xmlMapper.readValue(xml, parseToClass);
        return parsedObject;
    }
}
公共类主{
.
.
.
.
私有静态T unmarshallFromXML(字符串xml,类parseToClass)引发IOException{
XmlMapper XmlMapper=新的XmlMapper();
setVisibility(PropertyAccessor.ALL,jsonautodect.Visibility.NONE);
setVisibility(PropertyAccessor.FIELD,JsonAutoDetect.Visibility.ANY);
enable(SerializationFeature.INDENT_输出);
setDefaultSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
setSerializationInclusion(JsonInclude.Include.NON_NULL);
setDefaultUseWrapper(false);
disable(在未知属性上反序列化功能失败);
T parsedObject=xmlMapper.readValue(xml,parseToClass);
返回parsedObject;
}
}

请建议使用Jackson成功解析该映射的方法。

默认情况下,
map
在以下位置序列化为
XML

...
<key>value</key>
<key1>value1</key1>
...
并将
FileConfig
类中的属性更改为:

List<Entry> otherConfigs;
class Entry {
    private String key;
    private String value;

    // getters, setters, toString
}
List<Entry> otherConfigs;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class XmlApp {

  public static void main(String[] args) throws Exception {
    File xmlFile = new File("./test.xml");


    XmlMapper xmlMapper = new XmlMapper();

    FileConfig fileConfig = xmlMapper.readValue(xmlFile, FileConfig.class);
    System.out.println(fileConfig);
  }
}

class MapEntryDeserializer extends JsonDeserializer<Map<String, String>> {

    @Override
    public Map<String, String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        Map<String, String> map = new HashMap<>();

        JsonToken token;
        while ((token = p.nextToken()) != null) {
            if (token == JsonToken.FIELD_NAME) {
                if (p.getCurrentName().equals("entry")) {
                    p.nextToken();
                    JsonNode node = p.readValueAsTree();
                    map.put(node.get("key").asText(), node.get("value").asText());
                }
            }
        }
        return map;
    }
}

class FileConfig {

  protected Boolean whetherNotify = false;
  protected String url;
  protected String includes;

  @JsonDeserialize(using = MapEntryDeserializer.class)
  private Map<String, String> otherConfigs;

  // getters, setters, toString
}
FileConfig{whetherNotify=false, url='....some location....', includes='app.log', otherConfigs={pathType1=2, pathType=1}}