cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Shane Saunders <shanesaunders.em...@gmail.com>
Subject Re: JSON for JAX-RS configuration
Date Tue, 22 May 2012 17:37:55 GMT
Tom,

This is what I setup for a very basic sample project that I created for our
project to use as a starting point for JSON. I think your setup looks good,
but I did not use the JSON Provider parameters that you have setup. I built
this with CXF 2.5.2. It was able to (un)marshall the JSON correctly for me
on the server side and I used a similar setup for the WebClient class.

beans.xml:
...
    <jaxrs:server id="myServer" address="/sample">
        <jaxrs:providers>
            <ref bean="jsonProvider" />
        </jaxrs:providers>

        <jaxrs:serviceBeans>
            <ref bean="myBean" />
        </jaxrs:serviceBeans>
    </jaxrs:server>

    <bean id="myBean" class="com.hp.exstream.sample.SampleRestJsonImpl" />

    <bean id="jsonProvider"
class="org.apache.cxf.jaxrs.provider.JSONProvider">
        <property name="ignoreNamespaces" value="true" />
    </bean>
...

Web Service Interface class:

@Path("/")
public interface SampleRestJson {

    @POST
    @Produces("application/json")
    @Consumes("application/json")
    @ElementClass(response = ResponseData.class, request =
RequestData.class)
    @Path("/request")
    public ResponseData request1(RequestData req);
}


RequestData:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RequestData {

    private String stringData;
    private int intData;
...
}


ResponseData:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseData {

    private String stringData;
    private int intData;
...
}


HTH,
Shane


On Tue, May 22, 2012 at 10:49 AM, <tom.waterhouse@thomsonreuters.com> wrote:

> After spending a couple of days trying to get JAXB and JSON working with a
> JAX-RS service I found that the following was required.  It wasn't clear to
> me from the documentation or samples that explicit declarations of the JAXB
> objects in the service bean configuration was required.  My initial
> assumption was that there was a default JSONProvider instance that didn't
> require configuration, and that any object with the @XmlRootElement
> annotation could be marshaled.
>
> The following is the correct method for configuring a JAX-RS service using
> JSON as the communication protocol?
>
> Web resource interface:
> @Path("users")
> public interface RestUserService {
>
>       @GET
>       @Path("/{name}")
>       @Consumes("application/json")
>       @Produces("application/json")
>       public Response findUser(@PathParam("name") String name);
>
>       @POST
>       @Consumes("application/json")
>       @Produces("application/json")
>       public Response create(User user);
> }
>
>
> Spring bean definition:
>       <!-- This provider is required when Web resources are using JSON as
> the protocol -->
>       <bean id="jsonProvider"
> class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
>              <property name="singleJaxbContext" value="true" />
>              <property name="jaxbElementClassNames">
>                     <list>
>
> <value>com.thomsonreuters.services.userservice._2012_02_01.User</value>
>                     </list>
>              </property>
>    </bean>
>
>       <jaxrs:server id="restServices" address="/rest">
>              <jaxrs:serviceBeans>
>                     <ref bean="restUserService" />
>              </jaxrs:serviceBeans>
>
>              <jaxrs:providers>
>                     <ref bean="jsonProvider" />
>              </jaxrs:providers>
>       </jaxrs:server>
>
> Tom
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message