Saturday, December 9, 2017

Web Services



Web Services:  APIs that use HTTP protocol are “web services”



What are the various types of APIs?

  1. RPC style
  2. Graphql
  3. REST API

What is REST  API?
Architectural constraints

Six guiding constraints define a RESTful system. These constraints restrict the ways that the server can process and respond to client requests so that, by operating within these constraints, the system gains desirable non-functional properties, such as performance, scalability, simplicity, modifiability, visibility, portability, and reliability.[3] If a system violates any of the required constraints, it cannot be considered RESTful.


API complying to 6 rest constraints :
  1.  Client-server Architecture  
  2. Stateless ness
  3. Caching
  4. Layered System 
  5. Code on Demand
  6. Uniform Interface

The formal REST constraints are as follows:

Client-server architecture

The principle behind the client-server constraints is the separation of concerns. Separating the user interface concerns from the data storage concerns improves the portability of the user interfaces across multiple platforms. It also improves scalability by simplifying the server components. Perhaps most significant to the Web is that the separation allows the components to evolve independently, thus supporting the Internet-scale requirement of multiple organizational domains.[3

Statelessness

The client-server communication is constrained by no client context being stored on the server between requests. Each request from any client contains all the information necessary to service the request, and the session state is held in the client. The session state can be transferred by the server to another service such as a database to maintain a persistent state for a period and allow authentication. The client begins sending requests when it is ready to make the transition to a new state. While one or more requests are outstanding, the client is considered to be in transition. The representation of each application state contains links that can be used the next time the client chooses to initiate a new state-transition.[11]

Cacheability

As on the World Wide Web, clients and intermediaries can cache responses. Responses must, implicitly or explicitly, define themselves as either cacheable or non-cacheable to prevent clients from providing stale or inappropriate data in response to further requests. Well-managed caching partially or completely eliminates some client-server interactions, further improving scalability and performance.

Layered system

A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. If a proxy or load balancer is placed between the client and server, it won't affect their communications and there won't be a need to update the client or server code. Intermediary servers can improve system scalability by enabling load balancing and by providing shared caches. Also, security can be added as a layer on top of the web services, and then clearly separate business logic from security logic.[12] Adding security as a separate layer enforces security policies. Finally, it also means that a server can call multiple other servers to generate a response to the client.

Code on demand (optional)

Servers can temporarily extend or customize the functionality of a client by transferring executable code: for example, compiled components such as Java applets, or client-side scripts such as JavaScript.

Uniform interface

The uniform interface constraint is fundamental to the design of any RESTful system.[3] It simplifies and decouples the architecture, which enables each part to evolve independently. The four constraints for this uniform interface are:

Resource identification in requests
Individual resources are identified in requests, for example using URIs in RESTful Web services. The resources themselves are conceptually separate from the representations that are returned to the client. For example, the server could send data from its database as HTMLXML or as JSON—none of which are the server's internal representation.
Resource manipulation through representations
When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource's state.
Self-descriptive messages
Each message includes enough information to describe how to process the message. For example, which parser to invoke can be specified by a media type.[3]
Hypermedia as the engine of application state (HATEOAS)
Having accessed an initial URI for the REST application—analogous to a human Web user accessing the home page of a website—a REST client should then be able to use server-provided links dynamically to discover all the available resources it needs. As access proceeds, the server responds with text that includes hyperlinks to other resources that are currently available. There is no need for the client to be hard-coded with information regarding the structure or dynamics of the application.


What is resourse ?


Any information that can be named can be a resource: a document or image, a temporal service (e.g. “today’s weather in Los Angeles”), a collection of other resources, a non-virtual object (e.g., a person), and so on. In other words, any concept that might be the target of an author’s hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time

Naming Guide 


RESTful URI should refer to a resource that is a thing (noun).

Consistency is the key

Use consistent resource naming conventions and URI formatting for minimum ambiguity and maximum readability and maintainability. You may implement below design hints to achieve consistency:

  1. Use forward-slash (/) to indicate hierarchical relationships

  2. Do not use trailing forward-slash (/) in URIs

  3. Use hyphens (-) to improve the readability of URIs

  4. Do not use underscores ( _ )

  5. Use lowercase letters in URIs

  6. Do not use file extensions

resource archetypes into four categories (document, collection, store, and controller)





The Anatomy Of A Request

It’s important to know that a request is made up of four things:

  1. The endpoint
  2. The method
  3. The headers
  4. The data (or body)

What is Spring REST

The Spring framework also provides REST support. The Spring MVC module can be used to develop a REST service. It defines several annotations that you can use to develop a REST application.

What are the differences between JAX-RS and Spring REST

Although both JAX-RS and Spring REST can be used to create a RESTful service in Java, there are several differences between the two as follows:

Some important annotations in JAX-RS and Spring REST

JAX-RSSpring RESTUse of annotation
@Get@RequestMapping, @GetMappingUsed to specify that the method maps to an HTTP GET method
@Post@RequestMapping, @GetMappingUsed to specify that the method maps to an HTTP POST method
@Path@RequestMappingUsed to specify The URI that the method or class maps to
@QueryParam@RequestParamUsed to specify a query parameter
@PathParam@PathVariableUsed to specify a path parameter








Spring boot is very easy to use and it does a lot of things under the hood

What is Spring Boot?

Spring Boot is a Framework from “The Spring Team” to ease the bootstrapping and development of new Spring Applications.
It provides defaults for code and annotation configuration to quick start new Spring projects within no time. It follows “Opinionated Defaults Configuration” Approach to avoid lot of boilerplate code and configuration to improve Development, Unit Test and Integration Test Process.

Ways to create Spring BOOT project 
  1. Using Spring Boot CLI Tool
  2. Using Spring STS IDE
  3. Using Spring Initializr Website
Why Spring BOOT ?
  1. To increase Productivity.
  2. Easy
  3. Time reduction
  4. It avoids writing lots of boilerplate Code, Annotations and XML Configuration.
  5.  Easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.
  6. It follows “Opinionated Defaults Configuration” Approach to reduce Developer effort
  7. It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.
  8. It provides CLI (Command Line Interface) tool to develop and test Spring Boot(Java or Groovy) Applications from command prompt very easily and quickly.
  9. It provides lots of plugins to develop and test Spring Boot Applications very easily using Build Tools like Maven and Gradle
  10. It provides lots of plugins to work with embedded and in-memory Databases very easily.

Main Goal of Spring Boot:

The main goal of Spring Boot Framework is to reduce Development, Unit Test and Integration Test time and to ease the development of Production ready web applications very easily compared to existing Spring Framework, which really takes more time.
  • To avoid XML Configuration completely
  • To avoid defining more Annotation Configuration(It combined some existing Spring Framework Annotations to a simple and single Annotation)
  • To avoid writing lots of import statements
  • To provide some defaults to quick start new projects within no time.
  • To provide Opinionated Development approach.
 What are the main components of Spring Boot?
  1. Spring Boot Starters
  2. Spring Boot AutoConfigurator
  3. Spring Boot CLI
  4. Spring Boot Actuator
  5. Spring Initilizr
  6. Spring Boot IDEs

Spring Boot Starter


The main responsibility of Spring Boot Starter is to combine a group of common or related dependencies into single dependencies. We will explore this statement in detail with one example.
spring-boot-starter-web:

  • Spring core Jar file(spring-core-xx.jar)
  • Spring Web Jar file(spring-web-xx.jar)
  • Spring Web MVC Jar file(spring-webmvc-xx.jar)
  • Servlet Jar file(servlet-xx.jar)


Spring Boot AutoConfigurator

The main responsibility of Spring Boot AutoConfigurator is to reduce the Spring Configuration. If we develop Spring applications in Spring Boot, then We dont need to define single XML configuration and almost no or minimal Annotation configuration. Spring Boot AutoConfigurator component will take care of providing those information


@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Inherited @Configuration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplication

That is, @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiration.


Spring Boot CLI

Spring Boot CLI(Command Line Interface) is a Spring Boot software to run and test Spring Boot applications from command prompt. 

Spring Boot Actuator

Spring Boot Actuator components gives many features, but two major features are
  • Providing Management EndPoints to Spring Boot Applications.
  • Spring Boot Applications Metrics.





The Spring Team has provided a Web Interface for Spring Boot Initilizr at “http://start.spring.io/” to quick start the Development of Spring Boot Applications using Maven/Gradle build tool very easily.

 Spring Boot Initilizr

The main aim of Spring Boot Initilizr is to start new project development within no time. It provides all required project structure and base build script files to quick start Spring Boot application without wasting time. It reduces development time.
Spring Boot Initilizr is available in the following forms
  1. Spring Boot Initilizr With Web Interface
  2. Spring Boot Initilizr With IDEs/IDE Plugins
  3. Spring Boot Initilizr With Spring Boot CLI
  4. Spring Boot Initilizr With Third Party Tools
We will look into “Spring Boot Initilizr With Web Interface” now with some examples and will discuss rest of three options in my coming posts.



Spring Initilizr
Spring Boot IDEs


No comments:

Post a Comment