Evgeni Dimitrov Evgeni Dimitrov - 2 months ago 9x
Java Question

CXF SOAP service preserve space

I have CXF soap web service.

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

There is a filed in MyModel that can be a space (
" "
), but the parser trims the value and it's serialized as
wile I want
<mySpaceVal> </mySpaceVal>

How can I do that?

I tried to add

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

to MyModel so the parser can add xml:space="preserve" attribute.
But it adds a namespace
and displays the attribute as
and the element is displayed as an empty string instead of
" "


You can wrap your string value in <![CDATA[]]> section using jaxb adapter:

public final static String space = "...";

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

    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;