Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Sample REST Web Service Application

DZone's Guide to

Sample REST Web Service Application

Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

Step 1: Download cuubez from the Repository

The first step is to download the latest cuubez stable release from: http://www.cuubez.com/index.php/2014-05-20-11-01-54

Maven Repo

<dependency>
    <groupId>com.cuubez</groupId>
    <artifactId>cuubez-core</artifactId>
    <version>1.0.0</version>
</dependency>

Step 2: Add the following libraries to your Web application

Note: No need to add if you use maven dependency

 
  • com.thoughtworks.xstream
  • commons-logging
  • javax.servlet
  • javassist
  • javax.ws.rs
  • com.google.1code.gson

Step 3: Define listeners and bootstrap classes

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>cuubez</display-name>

<listener>
 <listener-class>
   com.cuubez.core.servlet.BootstrapContextListener
 </listener-class>
</listener>


<servlet-mapping>
 <servlet-name>cuubez</servlet-name>
 <url-pattern>/rest</url-pattern>
</servlet-mapping>

<servlet>
 <servlet-name>init</servlet-name>
 <servlet-class>
    com.cuubez.core.servlet.HttpServletDispatcher
 </servlet-class>
</servlet>

</web-app>
Step 4: Create your first RESTful service

In a REST based architecture everything is a resource. A resource is accessed via a common interface based on the HTTP standard methods (e.g., POST, GET, PUT or DELETE). Every resource should support the HTTP common operations. Resources are identified by global ID's (which are typically URIs). In the first example, we will recall a RESTful Web service which returns a User object when a certain path is request with an HTTP GET:

User object :

public class User {

    private String id;
    private String name;
    private int age;


    public User(String id, String name, int age) {
        this.name = id;
        this.age = age;
        this.name = name;
    }

    public String getId() {return id;}

    public int getAge() {return age;}

    public String getName() {return name;}

    public void setId(String id) {this.id = id;}

    public void setAge(int age) {this.age = age;}

    public void setName(String name) {this.name = name;}
}

REST service class :

@Path("/users")
public class UserDetail {

    @GET
    @Path("/{userId}")   
    @Produces(MediaType.APPLICATION_JSON)
    public User getUser(@PathParam(value = "userId")String userId) {
      User user = new User(userId, "jhone", 35);
      return user;
    }
}

The @Path annotation at class level is used to specify the base URL of the Web service. Then you can assign a @Path annotation at method level to specify the single action you want to invoke.

In this example, if you need to invoke the userDetail service, you have to use the following URL: (we suppose that the web application is named cuubez.war)

http://localhost:8080/cuubez/rest/users/id11001

would return:

{
"id":"id11001",
 "name": "jhone",
 "age": 35
}

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
java ,enterprise-integration ,rest ,integration

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}