有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Spring的JavaSOAP服务。转义特殊字符

我有一个使用Spring框架开发的SOAP web服务。每当请求包含一些无效数据时,我需要显示错误消息,如下所示

Error occurred. Invalid data for <Field Name>.

因此,我的名称验证代码如下所示。当名称字段没有传递值时,此错误将作为响应发送

Assert.notNull(name, "Error occurred. No value passed for the field <name>. ");

所以我所期望的结果是

Error occurred. No value passed for the field <name>.

但是soapui中的响应如下所示

Error occurred. No value passed for the field &lt;name>.

如何显示正确的<;soapui中的符号?我试过CDATA。但不确定接收者如何使用CDATA处理请求

SOAPUI中的CDATA消息如下所示

Error occurred. No value passed for the field <![CDATA[<]]name>.

共 (1) 个答案

  1. # 1 楼答案

    XML规范规定:

    The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they must be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; ", and must, for compatibility, be escaped using either " &gt; " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.

    因此,您需要在错误字符串中转义左尖括号:

    Error occurred. No value passed for the field &lt;name>.
    

    或者将整个错误字符串封装在CDATA区域中:

    <![CDATA[Error occurred. No value passed for the field <name>.]]>
    

    有关更多信息,请参见http://www.w3.org/TR/xml/#syntax