You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Celinio <ce...@yahoo.com> on 2011/02/04 09:51:43 UTC

Building REST style web services

Building REST style web services

Hi,
I am a bit new to CXF and wondering what is the recommended way to build REST style web services with CXF now.

I simply decorated my method with the @Get and @HttpResource annotations:

    @WebService
public interface IBMICalculator {
    
    @Get
    @HttpResource(location = "/computeBMI/{weight}/{height}")
    public  double computeBMI(@WebParam(name="weight") double weight, @WebParam(name="height") double height) ;
}

I imported the binding extension xml file in my spring xml configuration file, cxf.xml:
<import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml"/>

And i declared an endpoint :
 <jaxws:endpoint id="calcBMI"
                  implementor="com.company.bmi.services.IBMICalculatorImpl"
                  address="/cxfBmi"
                  bindingUri="http://apache.org/cxf/binding/http">
                  <jaxws:serviceFactory>
           <bean class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
               <property name="wrapped" value="true" />
           </bean>
       </jaxws:serviceFactory>
   </jaxws:endpoint>
   
It works like a charm. However I read that "This binding has been deprecated and is likely to be removed from CXF in one of its future releases"
https://cwiki.apache.org/CXF20DOC/http-binding.html

So what is the recommanded way to build REST style web services with CXF now ?
Thanks for helping.


      

Re: Building REST style web services

Posted by Celinio <ce...@yahoo.com>.
Thanks, I just found out this alternative way indeed.

--- On Fri, 2/4/11, Sergey Beryozkin <sb...@gmail.com> wrote:

From: Sergey Beryozkin <sb...@gmail.com>
Subject: Re: Building REST style web services
To: users@cxf.apache.org
Date: Friday, February 4, 2011, 3:14 AM

Hi

On Fri, Feb 4, 2011 at 8:51 AM, Celinio <ce...@yahoo.com> wrote:

> Building REST style web services
>
> Hi,
> I am a bit new to CXF and wondering what is the recommended way to build
> REST style web services with CXF now.
>
> I simply decorated my method with the @Get and @HttpResource annotations:
>
>     @WebService
> public interface IBMICalculator {
>
>     @Get
>     @HttpResource(location = "/computeBMI/{weight}/{height}")
>     public  double computeBMI(@WebParam(name="weight") double weight,
> @WebParam(name="height") double height) ;
> }
>
> I imported the binding extension xml file in my spring xml configuration
> file, cxf.xml:
> <import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml"/>
>
> And i declared an endpoint :
>  <jaxws:endpoint id="calcBMI"
>                   implementor="com.company.bmi.services.IBMICalculatorImpl"
>                   address="/cxfBmi"
>                   bindingUri="http://apache.org/cxf/binding/http">
>                   <jaxws:serviceFactory>
>            <bean
> class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
>                <property name="wrapped" value="true" />
>            </bean>
>        </jaxws:serviceFactory>
>    </jaxws:endpoint>
>

Here's an alternative approach, using JAX-RS

@Path("/")
public interface IBMICalculator {

    @GET
    @Path("/computeBMI/{weight}/{height}")
    @Produces("text/plain")
    public  double computeBMI(@QueryParam("weight") double weight,
@QueryParam("height") double height) ;
}

<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>

<jaxrs:endpoint serviceClass="com.company.bmi.services.IBMICalculatorImp
                          address="/cxfBmi"/>

though the endpoint declaration may be more involved.


>
> It works like a charm. However I read that "This binding has been
> deprecated and is likely to be removed from CXF in one of its future
> releases"
> https://cwiki.apache.org/CXF20DOC/http-binding.html
>
>
the reason it was deprecated was that it was maintained and unlikely to be
maintained


> So what is the recommanded way to build REST style web services with CXF
> now ?
>

Please see the JAX-RS section : http://cxf.apache.org/docs/jax-rs.html

The only restriction of JAX-RS compared to the HTTP binding is that no
wrapping/unwrapping is supported. Example,

@Produces("text/plain")
 public  double computeBMI(@QueryParam("weight") double weight,
@QueryParam("height") double height) ;

@Produces("text/plain") is the hint to the runtime that the 'double'
response value needs to be returned as is, without wrapping into something
like
<response>12.3</response>

If the response wrapping is needed then a custom JAX-RS provider can easily
deal with wrapping the response.

However, if we had say

@Consumes("text/xml")
@POST
public  void addItem(double weight, double height) ;

where the weight and height are not query parameters but have been unwrapped
from XML such as

<item>
<weight>123</weight>
<height>123</height>
</item>

then it definitely won't work in JAX-RS.
Having

@Consumes("text/xml")
@POST
public  void addItem(Item item) ;

will do better....


Hope that helps, Sergey


> Thanks for helping.
>
>
>



      

Re: Building REST style web services

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On Fri, Feb 4, 2011 at 8:51 AM, Celinio <ce...@yahoo.com> wrote:

> Building REST style web services
>
> Hi,
> I am a bit new to CXF and wondering what is the recommended way to build
> REST style web services with CXF now.
>
> I simply decorated my method with the @Get and @HttpResource annotations:
>
>     @WebService
> public interface IBMICalculator {
>
>     @Get
>     @HttpResource(location = "/computeBMI/{weight}/{height}")
>     public  double computeBMI(@WebParam(name="weight") double weight,
> @WebParam(name="height") double height) ;
> }
>
> I imported the binding extension xml file in my spring xml configuration
> file, cxf.xml:
> <import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml"/>
>
> And i declared an endpoint :
>  <jaxws:endpoint id="calcBMI"
>                   implementor="com.company.bmi.services.IBMICalculatorImpl"
>                   address="/cxfBmi"
>                   bindingUri="http://apache.org/cxf/binding/http">
>                   <jaxws:serviceFactory>
>            <bean
> class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
>                <property name="wrapped" value="true" />
>            </bean>
>        </jaxws:serviceFactory>
>    </jaxws:endpoint>
>

Here's an alternative approach, using JAX-RS

@Path("/")
public interface IBMICalculator {

    @GET
    @Path("/computeBMI/{weight}/{height}")
    @Produces("text/plain")
    public  double computeBMI(@QueryParam("weight") double weight,
@QueryParam("height") double height) ;
}

<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>

<jaxrs:endpoint serviceClass="com.company.bmi.services.IBMICalculatorImp
                          address="/cxfBmi"/>

though the endpoint declaration may be more involved.


>
> It works like a charm. However I read that "This binding has been
> deprecated and is likely to be removed from CXF in one of its future
> releases"
> https://cwiki.apache.org/CXF20DOC/http-binding.html
>
>
the reason it was deprecated was that it was maintained and unlikely to be
maintained


> So what is the recommanded way to build REST style web services with CXF
> now ?
>

Please see the JAX-RS section : http://cxf.apache.org/docs/jax-rs.html

The only restriction of JAX-RS compared to the HTTP binding is that no
wrapping/unwrapping is supported. Example,

@Produces("text/plain")
 public  double computeBMI(@QueryParam("weight") double weight,
@QueryParam("height") double height) ;

@Produces("text/plain") is the hint to the runtime that the 'double'
response value needs to be returned as is, without wrapping into something
like
<response>12.3</response>

If the response wrapping is needed then a custom JAX-RS provider can easily
deal with wrapping the response.

However, if we had say

@Consumes("text/xml")
@POST
public  void addItem(double weight, double height) ;

where the weight and height are not query parameters but have been unwrapped
from XML such as

<item>
<weight>123</weight>
<height>123</height>
</item>

then it definitely won't work in JAX-RS.
Having

@Consumes("text/xml")
@POST
public  void addItem(Item item) ;

will do better....


Hope that helps, Sergey


> Thanks for helping.
>
>
>