On 22/05/12 23:20, [email protected] wrote:
No, not working with OSGI.  Just using a standard Web application with Spring.  
I am using CXF 2.6.0 though, not sure if that should matter.

It does matter, in 2.6.0 the code for the 'optional' providers managing the formats such as JSON, Atom, or XML produced by non-JAXB bindings, are located in cxf-rt-rs-extension-providers modules, and the actual 3rd party dependencies are also optional. In time we can introduce more fine-grained modules but in 2.6.0 the cxf-rt-rs-extension-providers serves as a container for optional providers, as I was a bit concerned about introducing 5 or so new modules per every optional provider

http://cxf.apache.org/docs/jax-rs.html#JAX-RS-CXF2.6.x

Cheers, Sergey


Note that I did have to manually add the Jettison dependency, and after I did I 
received an error stating that the context did not know about my class.  From 
there I created the JSONProvider instance with the class identified in 
jaxbElementsClassName property and was able to get my Web resource to work.

-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]]
Sent: Tuesday, May 22, 2012 2:25 PM
To: [email protected]
Subject: Re: JSON for JAX-RS configuration

Thanks,indeed the default JSON Provider should manage XMLRootElement annotated 
classes without any additional configuration.

Tom, do you work with OSGI ? Jettison bundle may need to be installed there

Cheers, Sergey
On 22/05/12 18:37, Shane Saunders wrote:
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,<[email protected]>   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







--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to