Showing posts with label web services. Show all posts
Showing posts with label web services. Show all posts

Monday, July 16, 2018

what do you mean by Idempotent in REST APIs?

Problem statement: what do you mean by idempotent and/or safe methods in Rest APIs?
* idempotent = unchanged

In context of REST APIs, when making multiple identical requests has the same effect as making a single request – then that REST API is called idempotent.

When you design REST APIs, you must realize that API consumers can make mistakes. They can write client code in such a way that there can be duplicate requests as well. These duplicate requests may be unintentional as well as intentional some time (e.g. due to timeout or network issues). You have to design fault-tolerant APIs in such a way that duplicate requests do not leave the system unstable.

An idempotent HTTP method is a HTTP method that can be called many times without different outcomes. It would not matter if the method is called only once, or ten times over. The result should be the same. It essentially means that the result of a successful performed request is independent of the number of times it is executed. For example, in arithmetic, adding zero to a number is idempotent operation.

If you follow REST principles in designing API, you will have automatically have idempotent REST APIsfor GET, PUT, DELETE, HEAD, OPTIONS and TRACE http methods. Only POST APIs will not be idempotent.
  1. POST is NOT idempotent.
  2. GETPUTDELETEHEADOPTIONS and TRACE are idempotent.
Let’s analyze how above HTTP methods end up being idempotent – any why POST is not.
HTTP POST
Generally – not necessarily – POST APIs are used to create a new resource on server. So when you invoke the same POST request N times, you will have N new resources on server. So, POST is not idempotent.
HTTP GET, HEAD, OPTIONS and TRACE

GETHEADOPTIONS and TRACE methods NEVER change the resource state on server. They are purely for retrieving the resource representation or meta data at that point of time. So invoking multiple requests will not have any write operation on server, so GET, HEAD, OPTIONS and TRACE are idempotent.
HTTP PUT
Generally – not necessarily – PUT APIs are used to update the resource state. If you invoke a PUT API N times, very first request will update the resource; then rest N-1 requests will just overwrite the same resource state again and again – effectively not changing anything. Hence, PUT is idempotent.
HTTP DELETE
When you invoke N similar DELETE requests, first request will delete the resource and response will be 200 (OK) or 204 (No Content). Other N-1 requests will return 404 (Not Found). Clearly, the response is different from first request, but there is no change of state for any resource on server side because original resource is already deleted. So, DELETE is idempotent.
Please keep in mind if some systems may have DELETE APIs like this:
DELETE /item/last
In above case, calling operation N times will delete N resources – hence DELETE is not idempotent in this case. In this case, a good suggestion might be to change above API to POST – because POST is not idempotent.
POST /item/last

Saturday, June 2, 2018

https://javarevisited.blogspot.com/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html in Spring MVC and REST

Spring Restful Web Services Example with JSON, Jackson and Client Program

What are the difference between REST & SOAP?

Problem statement: what are the difference between REST & SOAP?
  • SOAP:
  1. SOAP stands for Simple Object Access Protocol
  2. SOAP can only works with XML format
  3. SOAP uses service interfaces to expose business logic. WSDL(Web service description language) file provides information that can be used to understand what services the web service can offer.
  4. JAX-WS is the java API for SOAP web services.
  5. SOAP does not support error handling
  6. SOAP requires more bandwidth and resource than REST. example
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle=" http://www.w3.org/2001/12/soap-encoding">
<soap:Body>
 <Demo.ishaanoneWebService xmlns="http://tempuri.org/">
   <EmployeeID>int</EmployeeID>
   </Demo.ishaanoneWebService>
 </soap:Body>
</SOAP-ENV:Envelope>
  1. SOAP defines its own security.
  2. SOAP is not lightweight and it require XML parsing
  • REST
  1. REST stands for REpresentational State Transfer
  2. REST can works with different format like XML, JSON, plain text, HTML, etc
  3. REST uses URI to expose business logic. For example, if there is an object which represents the data of an employee hosted on a URL can be accessed as http://demo.ishaanone.com/Employee
  4. JAX-RS is the java API for RESTful web services.
  5. REST support built-in error handling
  6. REST requires less bandwidth and resource than SOAP. for example {"city":"Mumbai","state":"Maharastra"}
  7. RESTful web services inherits security measures from the underlying transpor
  8. REST is lightweight and does not require XML parsing.
  9. REST is useful for restricted devices, such as mobile, for which the overhead of additional parameters are less (e.g., headers)

Sunday, May 6, 2018

What do you know about RESTful web services ?

Problem statement: What is RESTful web services?

REST stands for Representational State Transfer. REST is defined as the stateless client-server architectural style for developing application accessed over the web. When web services use HTTP methods to implement the concept of REST architecture, then it is known as RESTful Web services. In this architectural style, data and functionality are served as resources and is accessed by URI (Uniform Resource Identifiers).
RESTful web services enable web services to work best by inducing properties like
  1. Performance
  2. Scalability
  3. Modifiability

Web services?

Problem statement: what is web services?

Web services are client and server applications that communicate over the World Wide Web's (WWW) Hyper Text Transfer Protocol. Web services provide a standard means of inter operating between software applications running on a variety of platforms and frameworks.


web services: ref-1 
web service: ref-2

Friday, May 4, 2018

Spring Boot Rest API Web Application !!

Problem statement: Implementation of Spring boot rest api web application

How to create Spring Boot Project for Rest API web service own implementation

Method-I: 
step-1: go to eclipse / sts ide menu bar
step-2: select File --> New --> Spring Starter Project
step-3: by default pom.xml will add required spring boot dependency
step-4: create your own controller class and enjoy.

Method-II:
step-1: go to eclipse / sts ide menu bar
step-2: select File --> New --> Maven Project
step-3: add the <spring-boot-starter-parent >
step-4: add <spring-boot-starter-web> dependency and enjoy


  • Required code implementations:

[1]- pom.xml:
<project xmlns="http...>

        <groupId>com.ishaan</groupId>
<artifactId>maven_projects</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
       
        <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

        <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<dependencies>
<!-- This is a web application -->
<!-- Adds Tomcat and Spring MVC, along others, jackson-databind included
transitively -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat embedded container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-json -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.0.3.RELEASE</version>

</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

[2] - SpringBootWebApplication .java:


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

//Create an Application class
@SpringBootApplication(scanBasePackages={"com.ishaan.springboot"})
// same as @Configuration @EnableAutoConfiguration @ComponentScan
public class SpringBootWebApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
System.out.println("Hello Spring Boot !!");
}
}

// @SpringBootApplication  add
// @Configuration -tags the class as a source of bean definitions for the application context.

// @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, //other beans, and various property settings.

[3] - WebServiceRestController .java:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//Create a simple web application
@RestController
@RequestMapping("/api")
public class WebServiceRestController {

@RequestMapping(value = "/ping/", method = RequestMethod.GET)
public String ping() {
return "Ping success !!";
}
}

[4] - application.properties : (under src/main/resources)
# port number used for spring boot
server.port = 8090

[5] - application.yml: (under src/main/resources)                                        

        server:
            port: 8090
Note: Point [4] and point [5] are same thing, with different way of representation.

URL request to access resources: http://localhost:8090/api/ping/
Response from the TOMCAT server USING POSTMAN: Ping success !!



How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...