java - Linux 中 SOAP 请求的请求 xml 中的 UTF-8 编码

标签 java xml linux soap jaxb

我遇到了一个奇怪的问题。我有一个 SOAP 服务来存储用户数据,为此我调用一个服务并传递一个 XML 文档。问题是,当我在 Windows 上尝试时,XML 正确显示,并包含所有有效的 UTF-8 字符

但是我的服务器是 Linux 服务器,当它尝试时,从对象生成的 XML 包含一些特殊 UTF-8 字符的垃圾字符。

我使用 JAXB 进行 XML 转换。

这是我从对象到字符串使用的代码

    String result = null;
    JAXBContext jaxbContext = ContextFactoryManager.getInstance().createJAXBContext(SomeObject.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, JAXBUtils.JAXB_FORMATTED_OUTPUT);jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, JAXBUtils.JAXB_FRAGMENT);
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, JAXBUtils.UTF8);
    StringWriter writer = new StringWriter();
    jaxbMarshaller.marshal(inputObject, writer);
    result = writer.toString();

当我从本地服务器(来自 Windows)运行此代码时,它工作得很好,但是当此代码在服务器(来自 Linux)上运行时,它会将特殊字符(如 ö)转换为一些垃圾字符。

请告诉我出了什么问题?

最佳答案

好的,

终于找到原因了。它与操作系统没有任何关系。在服务器上,配置了tomcat级别Valve。该阀门正在读取请求参数(来自 CatalinaRequest)。

当访问此 URL 时,https://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/connector/Request.html#setCharacterEncoding%28java.lang.String%29

我发现在获取任何参数之前必须先设置字符编码。

关于java - Linux 中 SOAP 请求的请求 xml 中的 UTF-8 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26298935/

相关文章:

java - 在 Controller 中处理删除请求的正确方法是什么?

java - 在 hibernate 中使用带有条件的联接和子查询

android - MultipartEntity:无法为 StringBody 设置 header

c - 在标准输出中缓冲

GitHub 分发版的 Java DB 路径

java - 如何在 select 语句中从 JOOQ 模型调用 postgresql 函数?

android 按钮在 listview 之上

xml.Unmarshal 错误 : expected element type <dict> but have <plist>

linux - 我可以在虚拟机中加载 "developer image"吗?

Linux重命名文件为大写