Re: Developing REST interface as Cumulocity App

Follow

Originally posted by eickler on 2012-03-20 21:15:53

Hi there,

I had a few difficulties with the pre-provided Jersey 1.8 libraries, but what worked for me in the SDK is to use the latest Jersey bundle inside my project. In short:
  • Set up a plug-in project just like described in the "Hello, world!" application.
  • Download Jersey bundle and copy it into your new project: http://maven.java.net/service/local/artifact/maven/redirect?r=releases&g=com.sun.jersey&a=jersey-bundle&v=1.12&e=jar">http://maven.java.net/service/local/art … 1.12&e=jar.
  • Open the manifest file and, on the "Runtime" tab, add jersey-bundle-1.12.jar to the classpath. Go to the "Dependencies" tab and add Jersey's dependencies under "Imported Packages". Your manifest looks similar like this:
  • Web-ContextPath: /service
    Bundle-ClassPath: jersey-bundle-1.12.jar,
     .
    Import-Package: com.cumulocity.model.idtype;version="0.13.0",
     com.cumulocity.rest.representation;version="0.13.0",
     com.cumulocity.rest.representation.inventory;version="0.13.0",
     com.cumulocity.sdk.client;version="0.13.1",
     com.cumulocity.sdk.client.inventory;version="0.13.1",
     javax.servlet;version="2.5.0",
     javax.servlet.http;version="2.5.0",
     javax.ws.rs;version="1.1.0",
     javax.ws.rs.core;version="1.1.0",
     javax.ws.rs.ext;version="1.1.0",
     org.objectweb.asm;version="3.3.1"
     … plus any other stuff you might be using …
    
  • Create a resource class with normal JAX-RS annotations. A "Hello, world" style example:
  • 
    …
    @Path("/hello")
    public class HelloResource {
    	@GET
    	@Produces(MediaType.TEXT_PLAIN)
    	public String hello() throws SDKException {
    		Platform platform = new PlatformImpl(…);
    		InventoryApi inventory = platform.getInventoryApi();
    		com.cumulocity.sdk.client.inventory.ManagedObject mo = inventory.getManagedObject(new GId("1972"));
    		return "Hello, " + mo.get().getName();
    	}
    }
    
  • Create a subclass of javax.ws.rs.core.Application returning the resource class.
  • 
    …
    public class Application extends javax.ws.rs.core.Application {
        @Override
        public Set> getClasses() {
            Set> result = new HashSet>();
            result.add(HelloResource.class);
            return result;
        }
    }
    
  • Add the Jersey servlet to web.xml and configure it to use the Application class. Roughly like this:
  • 
    
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      Service
      
        REST Service
        com.sun.jersey.spi.container.servlet.ServletContainer
        
          javax.ws.rs.Application
          …fully qualified name of your Application class…
        
        1
      
      
        REST Service
        /*
      
    
    
  • Run the stuff. Now you should be able to send requests to your interface, e.g., using curl with the above example:
  • 
    curl http://localhost:8080/service/hello
    
    HTH! Cheers, André
Have more questions? Submit a request

Comments