Newsletter

How to Test (JAX-RS) RESTful Web Services

Web Services » on Jul 20, 2014 { 1 Comment } By Sivateja

In this article i will show you how to test RESTful web service (JAX-RS), so far we have learned how to create a RESTful service and testing GET and POST requests through some web browser.  But in real time projects we will use different tools to test RESTful web services.  If you would like to test JAX-RS with web browser you can use the following tools…

  • Postman [ Chrome Extension ]
  • REST Client [ Chrome Extension ]
  • Advanced REST Client [ Chrome Extension ]
  • Rest Client [ Firefox Add-On ]

If you would like to test JAX-RS in your local

  • RESTClient UI
  • SoupUi

In this tutorial i will show you how to test jax-rs with RESTClient UI

  • Click here to Download RESTClient UI
  • Open the above link and download restclient-ui-3.2.2-jar-with-dependencies.jar
  • We are done, you no need to do any configurations kind of things, Just double click on the downloaded jar file to run the application, it looks like…

Lets take an example with GET, POST, PUT, DELETE for testing the web service.

Required Files

  • pom.xml
  • web.xml
  • TestingRestfulWebService.java

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TestRestfulWebServiceExample</groupId>
  <artifactId>TestRestfulWebServiceExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        
    </dependencies>
 
      <build>
        <finalName>TestRestfulWebServiceExample</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerVersion>1.5</compilerVersion>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>TestRestfulWebServiceExample</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.java4s</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

TestingRestfulWebService.java

package com.java4s;
 
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.Response;
 
@Path("/customers")
public class TestingRestfulWebService {
 
    @GET
    @Produces("text/plain")
    @Path("{id}")
    public Response getCustomerDetails(@PathParam("id") String custId) {
        
         //CODE TO FETCH CUSTOMER DETAILS FROM THE DATABASE USING CUSTOMER ID
        String output = "Customer Details With ID "+custId+" Has Been fetched Successfully";
        return Response.status(200).entity(output).build();
     }
    
    @POST
    @Produces("text/plain")
    @Path("{id}")
    public Response insertCustomer(@PathParam("id") String custId) {
        
         //CODE TO INSERT CUSTOMER DETAILS USING CUSTOMER ID
        String output = "Customer Data With ID "+custId+" Has Been Saved Successfully";
        return Response.status(200).entity(output).build();
     }

    @PUT
    @Produces("text/plain")
    @Path("{id}")
    public Response updateCustomerDetails(@PathParam("id") String custId) {
        
         //CODE TO UPDATE CUSTOMER DETAILS USING CUSTOMER ID
        String output = "Customer Data With ID "+custId+" Has Been Updated Successfully";
        return Response.status(200).entity(output).build();
     }
    
    @DELETE
    @Produces("text/plain")
    @Path("{id}")
    public Response deleteCustomer(@PathParam("id") String custId) {
        
         //CODE TO DELETE CUSTOMER DETAILS USING CUSTOMER ID
        String output = "Customer With ID "+custId+" Has Been Deleted From the Database.";
        return Response.status(200).entity(output).build();
     }
    
}

Testing JAX-RS GET Request

  • Eclipse > Run the application > Now open Restclient UI
  • In the URL field enter  http://localhost:2013/TestRestfulWebServiceExample/rest/customers/100
  • Choose GET method in the ‘HTTP Method’ options > now hit the start button and check the output
  • Output

Testing JAX-RS POST Request

  • Choose POST method in the ‘HTTP Method’ options > Hit the start button and check the output
  • Output

Testing JAX-RS PUT Request

  • Choose PUT method in the ‘HTTP Method’ options > Hit the start button and check the output
  • Output

Testing JAX-RS DELETE Request

  • Choose DELETE method in the ‘HTTP Method’ options > Hit the start button and check the output
  • Output

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

One Response to “How to Test (JAX-RS) RESTful Web Services”
  1. rani says:

    Hello Sivateja,

    I have a real time scenario that i want to retrieve a merchant information from the list of merchants stored in db searching by business name first and if not found we should search by combination of fields, for ex,
    business name+phone number;
    business name+City;
    phone number+city+state; etc

    If you have an idea about this scenario using RESTFUL WEB SERVICES USING JERCY

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.