简体   繁体   中英

Apache CXF 2.4.6 to 2.5.3 upgrade

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM