Sunday, 25 January 2015

JAX-RS Annotations and First Program Example 1



JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture.

The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services

There are 11 Annotations.



In this example we are going to see following annotations.

@Path
@GET
@Produces

Example:
package com.sun.jersey.samples.helloworld.resources;

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

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {
    
    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getHelloMessage() {
        // Return some cliched textual content
        return "Hello World";
    }
}



Explanation:
  1. HelloWorldResource Class is a  root resource class.
    Root resource classes
     are POJOs that are either annotated with @PATH or have at least one method annotated with @Path or a request method designator, such as @GET, @PUT, @POST, or @DELETE. Resource methods are methods of a resource class annotated with a request method designator.
  2. @Path("/helloworld") : The Java class will be hosted at the URI path "/helloworld"
    URI path would be http://localhost:8080/helloworld/ OR 
    http://myservice.com/helloworld/(could be called from browser, we will see how later)
  3. @GET - the annotated Java method will process HTTP GET requests.
  4. @Produces  - annotation specifies the MIME media types, a resource can produce and send back to the client. 
    MIME (Multi-Purpose Internet Mail Extensions) is an extension of the original Internet e-mail protocol that lets people use the protocol to exchange different kinds of data files on the Internet: audio, video, images, application programs, and other kinds

    In this example, the Java method getHelloMessage() will produce representations identified by the MIME media type "text/plain", which is  returning value 
    "Hello World"


Responding to HTTP Methods and Requests
The behavior of a resource is determined by the HTTP methods (typically, GET, POST, PUT, DELETE) to which the resource is responding.


Wednesday, 21 January 2015

Java EE 6 web services technologies : JAX-WS and JAX-RS

We would be, in this post, focusing on creating web services with RESTful Web Services (JAX-RS)

Some backgrounds:
In Short, Java API for XML Web Services (JAX-WS) is a technology for building web services and clients that communicate using XML. JAX-WS allows developers to write message-oriented as well as Remote Procedure Call-oriented (RPC-oriented) web services.
In JAX-WS, a web service operation invocation is represented by an XML-based protocol, such as SOAP.

Web services are client and server applications that communicate over the World Wide Web’s (WWW) HyperText Transfer Protocol (HTTP). As per World Wide Web Consortium (W3C), web services provide a standard means of interoperating between software applications running on a variety of platforms and frameworks.

We can say application hosted in backend on server to serve data to clients

Examples: 



Labels:

Sunday, 18 January 2015

What is RESTFul Web Service

REST - Representational State Transfer

REST is an architecture style or design pattern used for creating web services.

RESTful Web services allow anything connected to a network (web servers, private intranets, smartphones, banking systems) to communicate with one another.

They communicate via a shared common communications protocol known as Hypertext Transfer Protocol (HTTP). 

The same HTTP verbs (GET, POST, PUT, DELETE etc.) used by web browsers 
 - to retrieve and display web pages, audio/video files, images etc. from remote servers and 
 - post data back to them when performing actions like filling out and submitting forms 


REST is used as a simpler alternative to SOAP and WSDL based Web services.