JAX-RS Tutorial: 1 min RESTFul service

This is not intended to explain the REST, but one of the best ways to create simplest web service in JAX-RS 311. However, you need to install maven as a prerequisite. Eclipse is the IDE for me.

mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2

This will prompt you for option to create project with different configurations. Select option 1, that is the simplest.

If you are suppose to go with Eclipse, then don’t forget to execute the following command, that is

mvn eclipse:eclipse

Then you can import this project as an existing project in the Eclipse.

In the service, important to provide the path in @path annotation. HelloWorldResource is the simplest web service.

package com.blogspot.ojitha.rest.example.first;

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

@Path("/hello")

public class HelloWorldResource {

	@GET
	@Produces("plain/text")
       private String hello(){
		return "Hello World";
	}
}

Before access the service, you need to execute the service, which is already written in to the Main.java file. Now access the service via browser, which is http://localhost:9998/hello

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python