1

I tried to update following code from Apache CXF 2.4.6 to Apache CXF 2.5.3:

@Path("/myresource")
public class MyResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Root get() {

        Root root = new Root();
        root.setName("Test");
        return root;
    };
}

@XmlRootElement(namespace = "http://www.my.org", name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Spring configuration:

<jaxrs:server address="/myPath" id="myID"
    <jaxrs:serviceBeans>
        <ref bean="myResource" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="org.apache.cxf.jaxrs.provider.JAXBElementProvider"/>
        <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
    </jaxrs:providers>
</jaxrs:server>

With Apache CXF 2.4.6 and Jackson 2.2.3 I get the right JSON:

{"name":"Test"}

With Apache CXF 2.5.3 and Jackson 2.2.3 I get a wrong JSON:

{"ns1.root":{"name":"Test"}}

Client:

JAXRSClientFactoryBean jaxrsClientFactoryBean = new JAXRSClientFactoryBean();
jaxrsClientFactoryBean.setAddress(address);
jaxrsClientFactoryBean.setProviders(Arrays.asList(new JacksonJaxbJsonProvider(), new JAXBElementProvider()));
jaxrsClientFactoryBean.setServiceClass(MyResource.class);
MyResource myResource = jaxrsClientFactoryBean.create(MyResource.class);
Root root = myResource.get();

On client-side (proxy client) I get an exception:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"ns1.root"). Expected elements are <{http://www.my.org}root>

I read the migration guide, but found no answer for my problem.

dur
  • 15,689
  • 25
  • 79
  • 125
  • 1
    Not sure what your setup is like, but maybe [this](http://stackoverflow.com/a/13306718/1324406) is what you're looking for? – Amber Sep 09 '15 at 17:06

1 Answers1

1

since you defined the root element as "root", this node is supposed to show up in the serialized json.

You can drop this root element by setting property dropRootName of your json provider, as described here.

http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-WrappingandUnwrappingJSONsequences

elakito
  • 68
  • 4