我有CXF Soap Web服务。

@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",并且该元素显示为空字符串而不是" "

最佳答案

您可以使用<![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;
    }
}

关于java - CXF SOAP服务保留空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35631214/

10-09 05:20