Skip to Content
Author's profile photo Pankaj Kumar

Develop and Deploy JAX-RS based REST application using CXF and Spring on SAP NetWeaver Cloud – 2

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


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 to 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;
public class App {
    public String ping(@PathParam("input") String input) {
        return input;
    public Response modifyJson(JsonBean input) {
 return Response.ok().entity(input).build();

No changes to

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=""
  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <bean class=""/>
  <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>
   <jaxrs:server id="services" address="/">
      <bean class="sample.restez.App" />
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>

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"?>
 <display-name>JAX-RS Simple Service</display-name>
 <description>JAX-RS Simple Service</description>

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


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 –


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.


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.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.