Test CXF Restful Services with Rest-Shell @Put @Get


This short post publishes a Restful Web Service and then tests @Put and @Post methods using rest-shell.

Create the Service

Follow the instructions from my previous post: Produce Json From Restful CXF Web Service.
Once you have done this, modify the CustomerService as follows:

package com.skills421.services;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.springframework.stereotype.Service;

import com.skills421.model.Customer;

@Service
@Path("/customer")
public class CustomerService
{
private Map<Integer,Customer> customers = new HashMap<Integer, Customer>();

@PUT
@Path("/add/{id}")
@Consumes("application/json")
public Response addCustomer(@PathParam("id") int id,Customer customer)
{
customers.put(id,customer);
return Response.status(Status.OK).build();
}

@GET
@Path("/find/{id}")
@Produces("application/json")
public Response find(@PathParam("id") int id)
{
Customer customer = customers.get(id);

return Response.status(Status.OK).entity(customer).type(MediaType.APPLICATION_JSON).build();
}

@GET
@Path("/default")
@Produces("application/json")
public Response getDefault()
{
Customer customer = new Customer(1,"Jon Doe",21);

return Response.status(Status.OK).entity(customer).type(MediaType.APPLICATION_JSON).build();
}
}

This provides methods to Put and Get data.

Test the Service using Rest-Shell

Open a terminal window and enter the following:

rest-shell
baseUri http://localhost:8080/SimpleCXFRest/rest/customer
get default

This will display the following:

005-rest-shell

Now enter the following:

put add/1 --data "{id:1,name:"Mark Doe",age:21}"

This will put Mark Doe to our service and display the following:

006-rest-shell

Finally, let’s check to see if our data was successfully uploaded by entering the following:

get find/1

and now we can see the following:

007-rest-shell
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s