JAX-RS Rest Service Example with Jersey in Java

In this tutorial we will show you the basic configuration needed to get a RESTful web service up and running. Get the required dependencies, create and expose methods and finally configure the servlet to the right path. Common name for mapping your servlet is /api/*. That’s it!

Project Structure

|    +--java
|        +--com
|            +--memorynotfound
|                +--rs
|                    |--HealthCheck.java
|    +--resources
|    +--webapp
|        +--WEB-INF
|            |--web.xml

Jersey Dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"




Note: If you want to use jersey version 2 you need to replace the jersey-servlet dependency with the following jersey-container-servlet dependency.


JAX-RS Rest Service

package com.memorynotfound.rs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

public class HealthCheck {

    public String getHealth() {
        return "alive and kicking";


Jersey Configuration web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">



Note: If you want to use jersey version 2 you need to replace the servlet definition with the following:



You can test the JAX-RS REST Service using URL: http://localhost:8080/jersey/api/healthcheck

  1. The jersey part is the path where our application is deployed
  2. The api part is the path where our jersey servlet is mapped
  3. The healthcheck part is the path where our health check rest service is deployed

Executing the RESTfull web service using curl

$ curl http://localhost:8080/jersey/api/healthcheck
alive and kicking

Inspecting the headers

$ curl -I http://localhost:8080/jersey/api/healthcheck
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html
Content-Length: 0
Date: Thu, 11 Dec 2014 16:49:41 GMT


JAX-RS RESTfull service

For more information on this topic you can read more on the following hyperlinks.



You may also like...