我正在使用JAX-WS服务。以下是请求类的一部分。
@XmlElement(name = "Answers")
protected String answers;
现在,在实际的SOAP请求中,需要将答案作为CDATA在xml中发送。答案有一个单独的存根类。因此,我将该类的对象编组为xml。我在CDATA标签中将其包围,如下所示:
xmlstr = "<![CDATA[" + xmlstr + "]]>";
因此,我的请求xml应如下所示:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<!-- Other tags -->
<Answers>
<![CDATA[
<TagOne></TagOne>
<TagTwo></TagTwo>
]]>
</Answers>
</S:Body>
</S:Envelope>
但是,当请求从SOAPLoggingHandler发送到服务器时,它看起来像这样:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<!-- Other tags -->
<Answers>
<![CDATA[
<TagOne></TagOne>
<TagTwo></TagTwo>
]]>
</Answers>
</S:Body>
</S:Envelope>
由于这些字符的转义,我收到的回复是“无效的答案xml格式”。我有两个问题:
xmlstr =“”是从bean创建CDATA xml的正确方法吗?如果没有,那么有没有标准的方法呢?
如果我想在没有转义的情况下发送请求中的CDATA部分,我应该对我的实现进行哪些更改?
如果需要其他任何内容,请告诉我。
答案 0 :(得分:0)
我认为这里的情况是你的java对象表示与你想在XML中看到的不完全匹配。假设您将JAXB与JAX-WS一起使用,那么您可以使用XmlJavaTypeAdapter批注为bean中的元素创建适配器类。
public class CDATAAdapter extends XmlAdapter<String, String> {
@Override
public String marshal(String v) throws Exception {
return "<![CDATA[" + v + "]]>";
}
@Override
public String unmarshal(String v) throws Exception {
return v;
}
}
在你的bean中:
@XmlElement(name = "Answers")
@XmlJavaTypeAdapter(value=CDATAAdapter.class)
protected String answers;