bluehallu
bluehallu

Reputation: 10275

Simple local restful web service

I've created a very basic iOS/Android application. The next step is to make the app load some data from a server. For now, all I need is to get an XML from which I'll be loading the data from a local server which will have a couple of simple functions. Which is the fastest approach to achieve this goal? I'm on a Mac machine, and I don't care about the language, although I had Java or Python in mind.

Upvotes: 0

Views: 1582

Answers (1)

yves amsellem
yves amsellem

Reputation: 7234

I highly recommend the use of Jersey. This Java framework is light, easy to work with and is the JAX-RS reference implementation. It produces XML and JSON out of the box. Its documentation is a good place to start.

What you need:

  • a Java Project
  • a web.xml
<web-app>
  <servlet>
    <servlet-name>jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
      </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>
  • some dependencies (jersey-core and jersey-server), using maven, editing the pom.xml is enough
<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-server</artifactId>
  <version>1.7</version>
</dependency>
  • a resource
@Path("/product")
public class ProductResource {

  @GET
  public Response getCallbackUrl() {
    Product product = new Product("bike");
    return Response.ok(product).build();
  }
}
  • a server. If you never use a Java server, using Jetty-Embedded is the simpler:
package com.xebia.server;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public class EmbeddedServer {

  static Server server;
  static int port = 8080;

  public static void main(String args[]) throws Exception {
      server = new Server(port);
      server.addHandler(new WebAppContext("src/main/webapp", "/"));
      server.start();
  }
}

And you're done

Upvotes: 2

Related Questions