Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Many of you may have noticed a gap in the documentation of Hana Cloud Platform development. There is a description of UI5 and how to build nice UIs with this HTML5 technology. And there is a description of how to build backend java based applications which starts with servlet technology?! Wouldn't it be nice to have UI5 and let it communicate with the Java backend and not to build servlets anymore?

In fact if HCP would support Java EE 7 Web Profile instead of version 6 you would get JAX-RS support out of the box and can use the JSONModel in UI5 to communicate with your HCP backend. With version 6 you can still use JAX-RS but you have to include it yourself in your application.

I want to describe here as an overview whats need to get this done. As normal you need a dynamic Web project in your Eclipse as a start point. I have successfully used Jersey/Jackson as the implementation of the JAX-RS 2.0 standard. If you have a maven project inlude the dependencies as described on Jersey project page. If you don't use maven just include the libs in WebContent/WEB-INF/lib as you would normally do.

So what do you need for JAX-RS to work for you? Lets assume you want to create one service class which responds to a HTTP GET on the relative path "/rest/ping" where "rest" shall be the path of all rest based calls and ping the specific part.

So you have to do some work in your web.xml first:


  <servlet>
    <servlet-name>com.sap.myapp.MyApplication</servlet-name>
    <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>com.sap.myapp.MyApplication</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

This defines a special servlet that you have to write later on and defines that all your REST based services are reachable under the path of "/rest/*" as we wanted to do.

So lets create the class com.sap.myapp.MyApplication which has to extend javax.ws.rs.core.Application, a standard class of JAX-RS. It should look like the following:


package com.sap.myapp;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

/**
* JAX-RS Application
*/
public class MyApplication extends Application {

  public Set<Class<?>> getClasses() {
    Set<Class<?>> s = new HashSet<Class<?>>();
    s.add(PingService.class);
    return s;
  }
}

Here you have to add EVERY service class you write. Normally you would divide your services in several classes. We now have to write the PingService thats added there.


/**
* REST
*/
package com.sap.myapp;
import javax.servlet.ServletException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* JAX-RS Service for ping
*/
@Path("/ping")
public class PingService {
  @GET
  @Path("/")
  @Produces(MediaType.TEXT_PLAIN)
  public Response ping() throws ServletException {
    return Response.ok().entity("pong").build();
  }
}

So this service listens on everything on the relative path "/ping". Together with the declaration in web.xml the relative path is now: /rest/ping/*

It has one method that is called on a HTTP GET request and it produces plain text. Normally you would produce JSON but thats something I will keep for part two. Just a hint in the return you can add any object in the entity call instead of a static string that gets transformed to JSON automatically!

Thats it you created your first REST service.

In Part 2 we add salt to the soup - EJBs to access your Database as described in the other tutorials!

5 Comments