Friday, February 3, 2017

RESTful WebServices in JAVA

JAX RS

Java API for RESTful Web Services (JAX-RS) is the Java API for creating REST web services. JAX-RS uses annotations to simplify the development and deployment of web services. JAX-RS is part of JDK, so you don’t need to include anything to use it’s annotations.


There are two major implementations of JAX-RS API-:

1. Jersey: Jersey is the reference implementation provided by Sun. For using Jersey as our JAX-RS implementation, all we need to configure its servlet in web.xml and add required dependencies. Note that JAX-RS API is part of JDK not Jersey, so we have to add its dependency jars in our application.

2. RESTEasy: RESTEasy is the JBoss project that provides JAX-RS implementation.

In the web services terms, REpresentational State Transfer (REST) is a stateless client-server architecture in which the web services are viewed as resources and can be identified by their URIs. Web service clients that want to use these resources access via globally defined set of remote methods that describe the action to be performed on the resource.

It consists of two components, REST server which provides access to the resources and a REST client which accesses and modify the REST resources.

In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol. REST isn't protocol specific, but when people talk about REST they usually mean REST over HTTP.

The response from server is considered as the representation of the resources. This representation can be generated from one resource or more number of resources.




Representational State Transfer refers to transferring "representations". You are using a "representation" of a resource to transfer resource state which lives on the server into application state on the client.




Why called rest?


A RESTful web service (also called a RESTful web API) is a web service implemented using HTTP and the principles of REST. It is a collection of resources, with four defined aspects:
  • the base URI for the web service, such as http://example.com/resources/
  • the Internet media type of the data supported by the web service. This is often XML but can be any other valid Internet media type providing that it is a valid hypertext standard.
  • the set of operations supported by the web service using HTTP methods (e.g., GET, PUT, POST, or DELETE).
  • The API must be hypertext driven

 <servlet>
    <servlet-name>RESTful Jersey CRUD Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.javapapers.webservices.rest.jersey</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>RESTful Jersey CRUD Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
In short, JAX RS has to declare resource in particular format and then client can access it using set pattern.

Basic Annotations:
@Path, @Pathparam,@GET, @PUT, @POST, @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

HTTP methods used in Restful webservices:
HTTP Method   Idempotent   Safe
OPTIONSyesyes
GETyesyes
HEADyesyes
PUTyesno
POSTnono
DELETEyesno
PATCHnono


Two best resource for JAX RS-:

http://javapapers.com/web-service/restful-services-crud-with-java-jax-rs-jersey/
http://www.mkyong.com/tutorials/jax-rs-tutorials/


Security in WEB services
During the past few years, the technology industry has been working on various XML-based security schemes to provide comprehensive and unified security schemes for Web services. These schemes include:
  • XML digital signature 
  • XML Encryption 
  • XKMS (XML Key Management Specification) 
  • XACML (Extensible Access Control Markup Language) 
  • SAML (Secure Assertion Markup Language) 
  • WS-Security (Web Services Security) 
  • ebXML Message Service 
  • The Liberty Alliance Project 
for secuirty topics;
http://www.javaworld.com/article/2073287/soa/secure-web-services.html
http://javaranch.com/journal/200603/WSSecurity.html



2 comments:

  1. https://docs.oracle.com/middleware/1212/owsm/OWSMC/owsm-security-concepts.htm#OWSMC2344

    ReplyDelete
  2. http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example/

    ReplyDelete

Interview Prep: Java full stack

 SOLID principle :  https://www.educative.io/answers/what-are-the-solid-principles-in-java Design Pattern:  Creational:  https://medium.com...