简体   繁体   中英

Objects as parameters in Apache CXF REST service method

What Exactly i need to do is to have a web service method in a REST API written in Apache CXF to accept a request like the following (preferably specifying a custom object as a parameter type)

{ "action":"read", "resource:"new resource" }

For now my method can do the same but it would expect a JSON string as the request body. But I need the service to describe the request parameters to the client. Meaning that in the wadl definition it should show the exact parameters that should be sent from the client. The ideal definition would be something similar to

<resource path="by-attrib">
  <method name="GET">
    <request>
     <param name="Accept" style="header" type="xs:string"/>
     <param name="Auth_Type" style="header" type="xs:string"/>
     <param name="Authorization" style="header" type="xs:string"/>
     <representation mediaType="application/json">
        <param name="action" style="plain" type="xs:string"/>
        <param name="resource" style="plain" type="xs:string"/>            
     </representation>
   </request>
   <response>
    <representation mediaType="application/json">
      <param name="result" style="plain" type="xs:string"/>
    </representation>
   </response>
</method>
</resource>

Is this possible with CXF? Please note that using @FormParam is not what I need, if I use form params, I get issues in sending a request using XML to the same method

Thank You

actually I found the answer

It is to use bean injection

described in their documentation itself, sorry for not RTFM

http://cxf.apache.org/docs/jax-rs-basics.html

under Parameter Beans

Basic idea is to use a java Bean (zero argument constructor with setters and getters) and add it to the web service parameters

You however need to specify one of @QueryParam @FormParam @PathParam for it to work

Example with CXF and jackson

The service interface (use POST, not GET)

@POST
@Path("/yourservice")
@Consumes({ MediaType.APPLICATION_JSON})
@Produces({
        MediaType.APPLICATION_JSON,
        MediaType.APPLICATION_XML})
public Result postYourService(YourData data) throws WebApplicationException;

The service impl (nothing special)

public Result postYourService(YourData data){
     Result r = new Result();
     r.setResult("result");
     return r; 
}

The data objects (Using jaxb to simplify encoding/decoding of json or xml)

@XmlRootElement(name = "YourData")
public class YourData {
    private String action;
    private String resource;
    //getter & setters
}

@XmlRootElement(name = "Result")
public class Result {
    private String result;
    //getter & setters
}

The spring configuration of CXF server and jackson. Jackson provider class depends on which version of CXF are you using. Therefore if the JacksonJaxbJsonProvider is not on your cxf package, look at the documentation

<jaxrs:server id="yourServiceREST" address="/services">
        <jaxrs:serviceBeans>
            <ref bean="yourService" />
        </jaxrs:serviceBeans>

        <jaxrs:providers>
            <!--<ref bean="authenticationFilter" />-->
            <!--<ref bean="exceptionMapper" />-->
            <!-- <ref bean="corsFilter" /> -->
            <ref bean="jackson" />
        </jaxrs:providers>
    </jaxrs:server>

<bean id="yourService" class="YourServiceImpl">
</bean>

<bean id="jackson" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />

Try to deploy and invoke to

POST /services/yourservice
{  "action":"read", "resource:"new resource"}

I do not know if WADL is going to be well generated because sometimes CXF fails. Be lucky!

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