gpt4 book ai didi

java - JAXB 编码为 StringWriter : Is it necassary to declare the string encoding (e. g。 UTF-8)?

转载 作者:行者123 更新时间:2023-11-30 10:48:20 24 4
gpt4 key购买 nike

使用这段代码:

        JAXBContext jaxbContext = JAXBContext.newInstance( aObj.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal( aObj, sw);
return sw.toString();

我正在将一个 JAXB 对象直接序列化为一个 StringWriter:我的示例表明像“ü”这样的两个字节 UTF-8 字符被完美序列化,即它们按预期包含在生成的 java 字符串中。稍后,当通过套接字发送时,生成的 java 字符串被序列化为 aXml.getBytes( "UTF-8")。

如果上述编码器没有明确声明 UTF-8 字符编码,会不会有危险?

最佳答案

这并不危险。

By default, the Marshaller will use UTF-8 encoding when generating XML data to a java.io.OutputStream, or a java.io.Writer. Use the setProperty API to change the output encoding used during these marshal operations. Client applications are expected to supply a valid character encoding name as defined in the W3C XML 1.0 Recommendation and supported by your Java Platform.

jaxb.encoding - 值必须是 java.lang.String编码 XML 数据时使用的输出编码。如果未指定此属性,Marshaller 将默认使用“UTF-8”。

Reference here

关于java - JAXB 编码为 StringWriter : Is it necassary to declare the string encoding (e. g。 UTF-8)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35840666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com