用Jackson库打印XML

时间:2022-12-01 13:43:24

I am trying to use Jackson library to serialize Java objects into XML by using JAXB annotations. However, I face an issue in pretty-printing the XML output.

我正在尝试使用Jackson库通过使用JAXB注释将Java对象序列化为XML。但是,我在打印XML输出时遇到了一个问题。

Here is my sample code usage:

这是我的示例代码用法:

ObjectMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String xml = mapper.writeValueAsString(person);

And, I'm seeing the below exception. The issue here is Jackson uses Stax2Writer as XML writer and Stax2 doesn't seem to support writing raw strings (in this case DefaultPrettyPrinter tries to write "\n" as raw string).

而且,我看到以下异常。这里的问题是Jackson使用Stax2Writer作为XML编写器,而Stax2似乎不支持编写原始字符串(在这种情况下,DefaultPrettyPrinter尝试将“\ n”写为原始字符串)。

    com.fasterxml.jackson.databind.JsonMappingException: Not implemented (through reference chain: xml.serialization.jackson.dto.Person["name"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:187)
    at com.fasterxml.jackson.dataformat.xml.ser.XmlBeanSerializer.serializeFields(XmlBeanSerializer.java:174)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider.serializeValue(XmlSerializerProvider.java:92)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2818)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2275)
    at xml.serialization.jackson.SerializationTest.main(SerializationTest.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.UnsupportedOperationException: Not implemented
    at org.codehaus.stax2.ri.Stax2WriterAdapter.writeRaw(Stax2WriterAdapter.java:380)
    at org.codehaus.stax2.ri.Stax2WriterAdapter.writeRaw(Stax2WriterAdapter.java:373)
    at com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter$Lf2SpacesIndenter.writeIndentation(DefaultXmlPrettyPrinter.java:498)
    at com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter.writeLeafElement(DefaultXmlPrettyPrinter.java:271)
    at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeString(ToXmlGenerator.java:489)
    at com.fasterxml.jackson.databind.ser.std.StringSerializer.serialize(StringSerializer.java:39)
    at com.fasterxml.jackson.databind.ser.std.StringSerializer.serialize(StringSerializer.java:21)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
    at com.fasterxml.jackson.dataformat.xml.ser.XmlBeanSerializer.serializeFields(XmlBeanSerializer.java:161)
    ... 10 more

Any thoughts on how to work it around?

有关如何使用它的任何想法?

2 个解决方案

#1


20  

According to the Stax2WriterAdapter's documentation thier default implementation is incomplete. Also comments in the code say that the writeRaw() method cannot be implemented using Stax 1.0 which is default for Java runtime.

根据Stax2WriterAdapter的文档,他们的默认实现是不完整的。代码中的注释也表示无法使用Stax 1.0实现writeRaw()方法,这是Java运行时的默认设置。

You should switch to use a Stax2 library as suggested on this wiki page.

您应该按照此Wiki页面上的建议切换到使用Stax2库。

After I added the following Maven dependency to my project, the example below started to work as expected:

在我将以下Maven依赖项添加到我的项目后,下面的示例开始按预期工作:

   <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.1.4</version>
   </dependency>

Code example:

代码示例:

public class JacksonXmlMapper {

    public static class Person {
        final public String name;

        public Person(String name) {
            this.name = name;
        }
    }
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new XmlMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        System.out.println(mapper.writeValueAsString(new Person("John")));
    }
}

Output:

输出:

<Person>
   <name>John</name>
</Person>

#2


-4  

Its caused because of the java.lang.UnsupportedOperationException:

它是由于java.lang.UnsupportedOperationException引起的:

if you implement the unimplemented method, your code should be working fine. for futher insights about the exception, please check this

如果您实现未实现的方法,您的代码应该正常工作。有关异常的进一步见解,请检查此

("java.lang.UnsupportedOperationException: Not supported yet.").

(“java.lang.UnsupportedOperationException:尚不支持。”)。

Thanks!!

谢谢!!

#1


20  

According to the Stax2WriterAdapter's documentation thier default implementation is incomplete. Also comments in the code say that the writeRaw() method cannot be implemented using Stax 1.0 which is default for Java runtime.

根据Stax2WriterAdapter的文档,他们的默认实现是不完整的。代码中的注释也表示无法使用Stax 1.0实现writeRaw()方法,这是Java运行时的默认设置。

You should switch to use a Stax2 library as suggested on this wiki page.

您应该按照此Wiki页面上的建议切换到使用Stax2库。

After I added the following Maven dependency to my project, the example below started to work as expected:

在我将以下Maven依赖项添加到我的项目后,下面的示例开始按预期工作:

   <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.1.4</version>
   </dependency>

Code example:

代码示例:

public class JacksonXmlMapper {

    public static class Person {
        final public String name;

        public Person(String name) {
            this.name = name;
        }
    }
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new XmlMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        System.out.println(mapper.writeValueAsString(new Person("John")));
    }
}

Output:

输出:

<Person>
   <name>John</name>
</Person>

#2


-4  

Its caused because of the java.lang.UnsupportedOperationException:

它是由于java.lang.UnsupportedOperationException引起的:

if you implement the unimplemented method, your code should be working fine. for futher insights about the exception, please check this

如果您实现未实现的方法,您的代码应该正常工作。有关异常的进一步见解,请检查此

("java.lang.UnsupportedOperationException: Not supported yet.").

(“java.lang.UnsupportedOperationException:尚不支持。”)。

Thanks!!

谢谢!!