# Creating a REST Web Service

Once you have configured your project for REST, you are ready to create your REST web service.

You can create a new REST web service class or generate a REST web service from an existing Java class using the Create RESTful Service wizard. The wizard creates the deployment files for you. After you create your web service, the final step is to deploy it.

When you create a RESTful web service, the JAX-RS Jersey library is automatically added to your REST project.

There are two types of REST web services available:

- How to Create a Patterned REST Web Service.

- How to Create a POJO REST Web Service.

## How to Create a Patterned REST Web Service

There are three options to choose from for your patterned REST web services:

- Single Source, - Creates a root-level JAX-RS resource class with GET and PUT methods.

- Resource/sub-resources - Creates a pair of JAX-RS resource classes. The 'item' class represents individual resources in a collection, the other class is for the 'container' that houses the collection. If the item resource URI is determined by the client, the item class is created using the PUT method in the item class, instead of the POST method.

- Entities Access - Creates JAX-RS compliant web services from JPA entity classes.

To create a Single Source REST web service using the wizard:

1. In the OEPE application choose New > Other > Web Services > REST Web Service. The Web Service Pattern dialog appears, as shown in Figure: REST Create Patterned Web Service - Single Root Resource.

2. The dialog has three REST pattern types from which to choose. Choose the Single Source pattern type and click Next, if you want to configure the REST methods, or Finish to use the defaults.

3. If you chose Next, the Web Service Structure dialog appears with the REST Java class and the methods, as shown in Figure: REST Create Patterned Web Service - Resource/sub-resources. You can accept the root path default or change it. Under Consumes/produces, choose the format in which you want your REST methods to be consumed or produced for the root resource class For more information see, Customizing Media Types for the Request and Response Messages.

4. Click Next. The Rest Application Class dialog appears. In this dialog you can accept the default, or specify the Package and Class name this is added to the REST configuration which is used at runtime to locate all the web services in the project.

REST Create Patterned Web Service - Single Root Resource

REST Create Patterned Web Service - Configure Methods

To create a Resource/Sub-Resources REST web service:

1. In the OEPE application choose New > Other > Web Services > REST Web Service. The Web Service Pattern dialog appears, as shown in Figure: REST Create Patterned Web Service - Resource/sub-resources.

2. The dialog has three REST pattern types from which to choose. Choose the Resource/sub-resources pattern type and click Next, to configure the REST methods, or Finish to use the defaults.

3. If you chose Next, the Web Service Structure dialog appears with the REST Java class and the methods, as shown in Figure: REST Create Patterned Web Service - Resource/sub-resources. You can accept the root path default or change it. Under Consumes/produces, choose the format in which you want your REST methods to be consumed or produced for the root resource class. For more information see, Customizing Media Types for the Request and Response Messages.

REST Create Patterned Web Service - Resource/sub-resources

To create a Entities Access REST web service

1. In the OEPE application choose New > Other > REST Web Service. The Web Service Pattern dialog appears.

2. The dialog has three REST pattern types from which to choose. Choose the Entities Access pattern type and click Next, if you want to configure the REST methods, or Finish to use the defaults.

3. If you chose Next, the Web Service Structure dialog appears with the REST Java class and the methods, as shown in Figure: REST Create Patterned Web Service - Resource/sub-resources. You can accept the root path default or change it. Under Consumes/produces, choose the format in which you want your REST methods to be consumed or produced for the root resource class For more information see, Customizing Media Types for the Request and Response Messages.

## How to Create a POJO REST Web Service

Once your project is configured for REST, (see Creating Projects Configured for REST) you can use annotations on your POJO s to define your REST web service URIs. You can either do this directly on the file, or using the Rest AnnotationProperties window. The @Path annotation defines the relative URI path for the resource, and can be defined as a constant or variable value (referred to as "URI path template"). You can add the @Path annotation at the class or method level.

To create REST web services in the Java source editor:

To define the URI as a constant value, pass a constant value to the @Path annotation. Preceding and ending slashes (/) are optional, as shown in Example: Using Annotations to Define the Resource Class URI as a Constant.

Using Annotations to Define the Resource Class URI as a Constant

    package samples.helloworld;
    import javax.ws.rs.Path;
    ...
    // Specifies the path to the RESTful service
    @Path("/helloworld")
    public class helloWorld {. . .}

In Example: Using Annotations to Define the Resource Class URI as a Variable shows the relative URI for the resource class defined using a variable, enclosed in braces.

Using Annotations to Define the Resource Class URI as a Variable

    package samples.helloworld;
    import javax.ws.rs.Path;
    ...
    // Specifies the path to the RESTful service
    @Path("/users/{username}")
    public class helloWorld {. . .}
    }

To create REST web services using the Annotation view

1. Create a POJO.

2. Open the Annotations view.

3. Put Cursor on the type declaration, this will orient the available annotations to this location.

4. Add the @Path Annotation, specify the value of your path, which is the URI for your resource class. This enable this class as a REST resource, as shown in Figure: Add REST to POJO - Annotations View.

5. Select applicable methods and annotate with the appropriate @Path, @Get, @Put, @Post, @Delete.

Add REST to POJO - Annotations View