首页 > 解决方案 > 使用 JAXB Marshaller 构建的 StringWrite 在使用 POSTMAN 查看时显示“<”而不是“<”

问题描述

我正在构建一个Web 服务应用程序项目。服务将数据存储在包含 bookInfo 的本地 XML 文件中。Web 服务有一种方法可以查看存储在此 XML 文件中的所有 booksInfo。因此,为了读取 XML,我使用JAXB并将其解组为 POJO

解组

AllItems items = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(AllItems.class);
        Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
        FileReader fileReader = new FileReader(new File("items.xml"));
        items = (AllItems) unMarshaller.unmarshal(fileReader);

//items here is an object of the supposed to be the root element

编组

在此之后,我使用以下代码将其编组为 StringWrite:

StringWriter stringWriter = new StringWriter();    
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(items, stringWriter);
} 
catch (JAXBException | FileNotFoundException e) {
    e.getMessage();
}

当我在控制台上查看输出时,一切看起来都很漂亮。如果我在 Web 浏览器上使用端点地址查看输出,它看起来仍然很漂亮。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<allItems>
<items>
    <saleInfo>
        <country>AU</country>
        <saleability>FOR_SALE</saleability>
    </saleInfo>
    <status>back order</status>
    <volumeInfo>
        <authors>Liz Ellis</authors>
        <industryIdentifiers>
            <identifier>9781760780364</identifier>
            <type>ISBN_13</type>
        </industryIdentifiers>
        <industryIdentifiers>
            <identifier>1760780367</identifier>
            <type>ISBN_10</type>
        </industryIdentifiers>
        <publishedDate>2018-04-24</publishedDate>
        <publisher>Macmillan Publishers Aus.</publisher>
        <title>If At First You Don't Conceive</title>
    </volumeInfo>
</items>

但是当我使用POSTMAN或尝试使用Web 服务器控制台查看它时,问题就开始了。

不需要的输出如何使应用程序接收“<”而不是 &alt;”

    &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?>
&lt;allItems>
    &lt;items>
        &lt;saleInfo>
            &lt;country>AU&lt;/country>
            &lt;saleability>FOR_SALE&lt;/saleability>
        &lt;/saleInfo>
        &lt;status>back order&lt;/status>
        &lt;volumeInfo>
            &lt;authors>Liz Ellis&lt;/authors>
            &lt;industryIdentifiers>
                &lt;identifier>9781760780364&lt;/identifier>
                &lt;type>ISBN_13&lt;/type>
            &lt;/industryIdentifiers>
            &lt;industryIdentifiers>
                &lt;identifier>1760780367&lt;/identifier>
                &lt;type>ISBN_10&lt;/type>
            &lt;/industryIdentifiers>
            &lt;publishedDate>2018-04-24&lt;/publishedDate>
            &lt;publisher>Macmillan Publishers Aus.&lt;/publisher>
            &lt;title>If At First You Don't Conceive&lt;/title>
        &lt;/volumeInfo>
    &lt;/items>
&lt;/allItems>

标签: javajaxbmarshallingaxis2unmarshalling

解决方案


推荐阅读