CXF SOAP服务保留空间

时间:2016-02-25 15:14:28

标签: java xml web-services soap jaxb

我有CXF肥皂网服务。

@Component
@WebService(endpointInterface = "com....MyWs", serviceName="MySrv")
public class MyWsImpl implements MyWs {
    @Override
    public List<MyModel> get(String customer) {
        List<MyModel> models = ...;
        return models;
    }
}

MyModel中有一个可以是空格(" ")的字段,但是解析器会修剪该值并将其序列化为<mySpaceVal></mySpaceVal>我希望<mySpaceVal> </mySpaceVal>

我该怎么做?

我尝试添加

@XmlAttribute(name="space", namespace="xml")
public final static String space = "preserve";

到MyModel,因此解析器可以添加xml:space =“preserve”属性。 但它添加了一个名称空间ns3="xml"并将该属性显示为ns3:space="preserve",该元素显示为空字符串而不是" "

1 个答案:

答案 0 :(得分:1)

您可以使用<![CDATA[]]>适配器将您的字符串值包装在jaxb部分中:

@XmlJavaTypeAdapter(CDATAXmlAdapter.class)
public final static String space = "...";

private static class CDATAXmlAdapter extends XmlAdapter<String, String> {
    @Override
    public String marshal(final String value) throws Exception {
        // you may want to apply additional value escaping to avoid
        // CDATA nesting problem
        return "<![CDATA[" + value + "]]>";
    }

    @Override
    public String unmarshal(final String value) throws Exception {
        // If you expect that server will send CDATA as well
        // then you need to strip CDATA from value
        return value;
    }
}