In the first part we used Maven ‘s org.apache.cxf.archetype to generate an Eclipse project that created a starter application that provides REST based service using JAX-RS. Here is the structure of the application that was generated

How it all works – web.xml has the references for CXF servlet that acts as the dispatcher servlet . This file also points to beans.xml .  beans.xml is CXF configuration file and points to HelloWorld class that actually provide/implement the needed services. JsonBean is the helper bean that represents the objects being passed.

One important thing that we haven’t talked about is the dependencies that were automatically taken for us by Maven. If you look under Libraries you would find all the jars that are automatically included

/wp-content/uploads/2012/08/7_130913.jpg

It won’t be much fun if we used the autogenerated code as it is. So let us change it a little bit for fun. First rename the HelloWorld.java to App.java which is shown below. On line 9 the @Path annotation was for /hello we changed it to /services also on line 20 we changed /jsonBean to /json

package sample.restez;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/services")
public class App {
    @GET
    @Path("/echo/{input}")
    @Produces("text/plain")
    public String ping(@PathParam("input") String input) {
        return input;
    }
    @POST
    @Produces("application/json")
    @Consumes("application/json")
    @Path("/json")
    public Response modifyJson(JsonBean input) {
 input.setVal2(input.getVal1());
 return Response.ok().entity(input).build();
    }
}

No changes to JsonBean.java

package sample.restez;
public class JsonBean {
    private String val1;
    private String val2;
    public String getVal1() {
 return val1;
    }
    public void setVal1(String val1) {
 this.val1 = val1;
    }
    public String getVal2() {
 return val2;
    }
    public void setVal2(String val2) {
 this.val2 = val2;
    }
}

beans.xml is the spring like CXF configuration clas. The only change we make is to make sure that it points to App class rather than the original HelloWorld class on line 19th

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <context:property-placeholder/>
  <context:annotation-config/>
  <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"/>
  <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>
   <jaxrs:server id="services" address="/">
    <jaxrs:serviceBeans>
      <bean class="sample.restez.App" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
    </jaxrs:providers>
    </jaxrs:server>
</beans>

Finally web.xml is the typical web.xml that uses the CXF servlet provided by CXF framework. Another point to note is the reference to beans.xml on line 11

<?xml version="1.0" encoding="utf-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
 <display-name>JAX-RS Simple Service</display-name>
 <description>JAX-RS Simple Service</description>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>WEB-INF/beans.xml</param-value>
 </context-param>
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <servlet>
  <servlet-name>CXFServlet</servlet-name>
  <servlet-class>
   org.apache.cxf.transport.servlet.CXFServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>CXFServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>

Now we are ready to deploy this slightly modified app. First deploy it to your local NWCloud instance by doing the “Run as – Run on Server”. On success Eclipse might launch the browser with the URL http://localhost:8080/restez which will not work. To do the first service test launch the following URL, it is an echo service

http://localhost:8080/restez/services/echo/RepeatAfterMe

you should see RepeatAfterMe in the browser. To test the JSON service we would use the “Advanced REST Client” extension for chrome. We would use the following URL –

https://localhost:8080/restez/services/json

As you can see we are using POST method and posting a Content-Type of application/json with the value of {“val1″:”JSON-REPEAT”} and in the result we get two values in the json format.

/wp-content/uploads/2012/08/8_130914.jpg

So there you have it, you have just created a REST based JAX-RS application that has two services –  echo a plain text based echo and json a similar service with json format. Now you can further adapt this example code for your business scenario.  Once these start working locally you can deploy them on the cloud.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply