Goal

This article describes how to create a simple rest web service without using a web container (stand alone).It uses the Java BuiltIn HttpServer and JAX-RS.

Used technologies

JDK 1.8
Maven 3.2
Maven dependencies: jersey 1.18.4

1. pom.xml

  <dependencies>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-bundle</artifactId>
			<version>1.18.4</version>
		</dependency>
	</dependencies>

2. Resource: HelloWorld

package com.wstutorial.rest;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("hello")
public class HelloWorldResource {
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String message() {
		return "hello World! ";
	}
}

3. Rest Starter

package com.wstutorial.rest;

import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;

public class StartRestServer {
	public static void main(String[] args) {
		HttpServer server;
		try {
			server = HttpServerFactory.create( "http://localhost:10080/rest" );
			server.start();
		}catch (Exception e) {
			System.out.println("Errormessage : " + e.getMessage());
		}

	}
}

4. Call the url

http://localhost:10080/rest/hello