gpt4 book ai didi

java - CXF SOAP 服务保留空间

转载 作者:行者123 更新时间:2023-11-30 10:48:49 26 4
gpt4 key购买 nike

我有 CXF SOAP 网络服务。

@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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com