我正在使用Apache CXF在服务器端实现一些Web服务。我必须实现一个WebService,它返回一个字符串(Holder),其中一些值由制表符分隔。 Apache CXF将字符选项卡编码为选项卡,但我们的客户端(无法更改...)不接受它,只读取编码为&#9的选项卡; 。
所以我试着简单地在字符串上创建一个replaceAll来改变\ t为	但是Marshaller上的escapeHandler将它更改为& amp; amp; amp; amp; amp;#9;
然后我尝试创建customCharacterEscapeHandler并在marshall com.sun.xml.bind.marshaller.CharacterEscapeHandler属性中设置。
<jaxws:endpoint
id="wsContainer"
implementor="com.xxxxx.xxxxx.xxxxx.webServices.impl.EOSWebServiceImpl"
address="/ws" >
<jaxws:dataBinding>
<bean class="org.apache.cxf.jaxb.JAXBDataBinding">
<property name="marshallerProperties">
<map>
<entry key="jaxb.encoding" value="UTF-8"/>
<entry key="com.sun.xml.bind.marshaller.CharacterEscapeHandler" value-ref="customCharacterEscapeHandler"/>
</map>
</property>
</bean>
</jaxws:dataBinding>
</jaxws:endpoint>
我的customCharacterEscapeHandler是:
public class CustomCharacterEscapeHandler implements CharacterEscapeHandler {
private final CharsetEncoder encoder;
public CustomCharacterEscapeHandler(String charsetName) {
this.encoder = Charset.forName(charsetName).newEncoder();
}
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
int limit = start+length;
for (int i = start; i < limit; i++) {
switch (ch[i]) {
case SoapUtils.SOAP_FIELD_SEP: //Cambios sobre el NioEscapeHandler para escapar tabuladores y saltos de linea
out.write("	");
break;
case SoapUtils.SOAP_RSEP:
out.write(" ");
break;
case '&':
out.write("&");
break;
case '<':
out.write("<");
break;
case '>':
out.write(">");
break;
case '\"':
if (isAttVal) {
out.write(""");
} else {
out.write('\"');
}
break;
default:
if( encoder.canEncode(ch[i]) ) {
out.write(ch[i]);
} else {
out.write("&#");
out.write(Integer.toString(ch[i]));
out.write(';');
}
}
}
}
}
这应该可以,但它没有,因为这个escapeHandler正确替换了tab字符,但是在此之后运行了一些其他的escapeHandler并再次替换'&amp;'性格,所以我有&amp; amp;#9;再次......
如何在客户端输入编码为&amp;#9的标签页? ??
PS:我正在使用Apache CXF 2.5.X
答案 0 :(得分:2)
我也有这个问题,根据我的观察,似乎使用CXF StaxOutInterceptor的XMLStreamWriter不支持JAXB CharacterEscapeHandler。
我通过向StaxOutInterceptor注入自定义org.codehaus.stax2.io.EscapingWriterFactory解决了这个问题。
这里是自定义EscapingWriterFactory的实现:
public class CustomXmlEscapingWriterFactory implements
org.codehaus.stax2.io.EscapingWriterFactory{
public Writer createEscapingWriterFor(final Writer out, String enc)
throws UnsupportedEncodingException {
return new Writer(){
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
String escapedStr = StringEscapeUtils.
escapeXml(new String(cbuf)).trim();
out.write(escapedStr.toCharArray(), off, escapedStr.toCharArray().length);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
out.close();
}
};
}
public Writer createEscapingWriterFor(OutputStream out, String enc)
throws UnsupportedEncodingException {
throw new IllegalArgumentException("not supported");
}
}
这是注入自定义EscapingWriterFactory的CXF拦截器
public class CustomXmlEscapingWriterInterceptor extends AbstractPhaseInterceptor<Message> {
public CustomXmlEscapingWriterInterceptor() {
super(Phase.PRE_STREAM);
addBefore(StaxOutInterceptor.class.getName());
}
@Override
public void handleMessage(Message message) {
WstxOutputFactory factory = new WstxOutputFactory();
factory.setProperty(XMLOutputFactory2.P_TEXT_ESCAPER, new CustomXmlEscapingWriterFactory());
message.put(XMLOutputFactory.class.getName(), factory);
}
}