Restful JSON Hello world example using 3.x web container(No web.xml required) Webservices JAX-RS tutorial



Step 1: Register your root path, rest controllers and its dependent providers

@ApplicationPath("/resource")

            Identifies the application path that serves as the base URI for all resource URIs provided by Path. May only be applied to a subclass of Application.

            When published in a Servlet container, the value of the application path may be overridden using a servlet-mapping element in the web.xml.


Code

package com.candidjava.webservices.config;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

import com.candidjava.webservices.controller.RegisterProcess;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

@ApplicationPath("/resource")
public class MyApplication extends ResourceConfig
{
	public MyApplication()
	{
		System.out.println("Resource Loaded..!");
		register(JacksonJsonProvider.class);
		registerClasses(RegisterProcess.class);
	}

}

Step 2: Create new controller to handle GET and POST request

@Path

Identifies the URI path that a resource class or class method will serve requests for. Paths are relative.


package com.candidjava.webservices.controller;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/RegisterProcess")
public class RegisterProcess
{
	@GET	
	@Produces(MediaType.APPLICATION_JSON)
	public Response getRegisterDetails()
	{
		String msg="You Clicked Get Request";

		return Response.ok().status(200).entity(msg).build();

	}

	// *************************************************

	@POST	
	@Produces(MediaType.APPLICATION_JSON)
	public Response getRegisterDetails1()
	{
		
		String msg="You Clicked Post Request";
		return Response.ok().status(200).entity(msg).build();

	}

	// *************************************************

	
}


Url to access the service

http://localhost:8080/jersey-Basic/resource/RegisterProcess?btn_register=Register


Screenshot

            

            


Download

Jersey Hello world maven zip

Jersey Hello world war




Related Post

Comments


©candidjava.com