Blogs On Programming

Swagger

Swagger

by Vibrant Publishers on May 22, 2022
Introduction   In this world of constantly changing specifications and requirements, it’s not an easy task to be updated with your web services. Meticulous planning and documentation is a must to win the race. There are a few tools in the market that will help you do the job. We will discuss one such tool Swagger – an API documentation and development toolset.     Swagger Swagger is not just a platform, but a set of tools that will help you in the process of documenting and developing APIs. All these APIs follow the Open API specification standard. Their official website (www.swagger.io) describes three different toolsets:   Swagger Editor Swagger UI Swagger Codegen     Let us have a detailed look into these toolsets: Swagger Editor The swagger editor is responsible for designing and creating APIs based on the Open API specification. This editor can be installed on your computer and you can use it by logging into your swagger account. The editor has an intelligent auto-completion feature and has a visual editor feature where you can interact with the API during its creation time itself. Below is the screenshot from the swagger editor.         Swagger UI The swagger UI is another tool that will allow you to visually see and interact with the APIs. These APIs can be created by Swagger Editor or there is provision for uploading other APIs as well. Once the API is given for the tool, it will automatically generate the visual schema of your API. One of the very useful features of this tool is that it will also create documentation for your API. This is very useful while implementing the API in the back end or at the client-side. You can execute the APIs from there itself and test its functionality as well.     Swagger Codegen The Codegen tool takes care of defining the servers and client-side SDKs for the API that you are building. In this way you don’t need to worry about those aspects while developing and API. It supports over 20 programming languages and the creation of these stubs and SDKs are quite an easy task in Codgen. This will help you to focus only on developing APIs than thinking about the client and server implementations.     Opening an account in Swagger An account in Swagger is quite easy to start. Just visit the website www.swagger.io and sign up for an account by providing some basic details. You can also open an account providing your Github credentials. Once you create an account, you will be taken into the swagger hub dashboard. Below is the screenshot of the same.         From the dashboard, you can create or import your APIs. These APIs can be public or private according to your requirement. If you need other teams from swagger to see and collaborate on the API development, then they have to be public.   Once you are working on your APIs, the view will be changed to something as shown below:         Here, you have access to all the tools, the editor, UI, API docs and Codegen. This will make working with the entire process a seamless experience. Once the API is created, there will be an option to download the corresponding server stub and client SDK.     The default account is free which limits the use only to one user per team. If you need more paid options available. Summary Swagger is a set of tools that is targeted towards API development and documentation. The visual editor and codegen tools are some of the compelling features of this platform. As an API developer, it is worth trying swagger for your use cases.
POSTMAN

POSTMAN

by Vibrant Publishers on May 22, 2022
Introduction   APIs are the heart of every web service. When we deal with enterprise web services, there will be requirements for building thousands of APIs and it is not a simple task for the developer to create all of them manually. To resolve this problem there are many API tools available which will handle the complete lifecycle of an API. In this article, we will discuss such a tool called POSTMAN.   POSTMAN According to the official website (www.postman.com), postman is a collaboration platform for API development. Yes, it allows multiple people to come together and work on creating APIs for their applications. Apart from this, it’s a platform that will take care of all the processes involved in an API creation and management.   Use of POSTMAN in the API world As mentioned above the platform takes care of all requirements in API development. Let us take a look at some of the features the postman offers:   Accessibility: The platform is a hosted service and all you need to start using it is an account in postman. You can either use the browser to login to postman or use their desktop app and start working.   Organized: The APIs that you create with postman can be organized well with the feature called collections. They can be placed in folders and subfolders based on your projects.   Monitoring: Another exciting feature of the postman is the power to monitor the APIs that you have created. This will make your life much easier due to the instant notification of failures in any of your API services.   Testing: Nothing is solid unless you test it thoroughly. Postman offers both manual and automated testing of your APIs. You can define test cases and discuss with your team over the platform itself and put the tests into action. Also, these tests can be integrated into your CI/CD pipeline.   Versioning: You might want to provide APIs for different customers that utilize your services. Or there can be different releases of the same API whenever your application releases new versions. In these cases versioning your API is important. The postman platform offers better tagging and versioning provisions that you and your team can utilize effectively.   Getting started with POSTMAN Inorder to use postman, you need to sign up for an account first. Below is the login/signup screen of the postman.         Once you have an account you can download the postman application and start building your APIs, Test cases, etc.   In postman you have the choice of selecting different plans based on your requirement and pricing. Initially you will be provided with a Free Plan where there are certain limitations on the number of API that you can create and manage. When your requirements are high and there is a team working with you, then it’s good to choose a Team, Business or Enterprise plan as per your convenience. Below is the screenshot of different plans that Postman offers.        Using POSTMAN After successfully creating an account in postman, you can download the client application on your desktop and start working with it.   Once you login to your postman application, the dashboard will look like the one below.         Here you can create your APIs, put them into collections and you can perform monitoring, testing, etc. on your APIs. The UI is very intuitive and can be learned quite quickly.   You can create APIs based on JSON or YAML schema formats. Also, there is support for Open API, GraphQL and RAML schemas.   There is a very detailed tutorial on how to use postman on their official website: https://learning.postman.com. You must pay a visit here if you want to learn about the platform in detail.   Summary Postman is a platform to create and manage the lifecycle of  APIs. It is versatile and has a lot of features including team collaboration and monitoring etc. Using postman for your project will not only save time but will provide a clean and neat way of managing your APIs as well.
Components required for JMS

Components required for JMS

by Vibrant Publishers on May 19, 2022
JMS refers to Java Messaging Service which allows the software developers to loosely couple applications. It has an API (Application Programming Interface) through which we can receive, send, read, and create enterprise system messages. JMS is a message oriented product which is quite expensive and complex. It provides an interface (Java) to write infrastructure code and allow solutions to be built easily and quickly.   Following are the main components of JMS: Administered: ObjectsDestinationConnectionFactory Connection Session Objects: MessageProducer, MessageConsumer, MessageListener, MessageSelector   The component relationship could be explained with the below diagram.   1) Administered Objects:   Following are called as the administered objects as they are created by the JMS provider administrator: Destination ConnectionFactory     Destination:   This object has the configuration information provided by JMS provider. This object is used by the Client to specify the destination for messages to be send and location to receive the messages.   There are 2 types of Interfaces Destination uses. They are: Queue – It belongs to PTP (Point To Point) model Topic – It belongs to pub/sub (Publish / Subscribe) model     Connection Factory:   This object has the connection configuration information (IP address) provided by JMS provider. The connection is obtained through the Java Naming Directory Interface (JNDI) which enables the client to create a connection and get connected to the JMS server.     2) Connection: This component provides the connection (physical) to the JMS server. The physical connection is obtained with the help of ConnectionFactory object which provides the instances of configuration details to connect to the JMS server.     3) Session: This component is responsible to receiving and sending messages, and for handling acknowledgements and managing transactions with the help of JMS objects.   4) Objects:   It refers to the JMS object which are maintained and created by the admin that are used by the JMS clients. The following objects are used to receive and create messages in JMS: MessageConsumer MessageProducer MessageSelector:  This is used to filter the message that does not meet the specified criteria. MessageListener:  This is used to process and receive messages asynchronously     MessageConsumer:   MessageConsumer is for receiving messages synchronously or asynchronously to a particular destination which is created by the session object.   In case of synchronous communication, the consumer calls the any one of the methods receive or receiveNoWait and in case of asynchronous communication, the client registers MessageListener and starts consumer.   Almost all the messages in Java Messaging Service are exchanged asynchronously between clients where the producer never receive acknowledgement from consumer.     MessageProducer: MessageProducer is for sending messages either via PTP or through pub/sub. In PTP (Point-To-Point) model, the destination is known as queue and in pub/sub model, the destination is known as topic.   In producer, we can set the delivery mode either as NON-PERSISTENT or PERSISTENT using the setDeliveryMode method. Non-Persistent has less overhead as the messages are not logged in the server or database.   We can also use the setPriority method to set the message priority. 0 indicates low priority and 9 indicates high priority. We can also use setTimeToLive method to mention the life time of the message in milliseconds.     MessageSelector:   MessageSelector belongs to String object which is used to filter the message that does not fit into the specified criteria.   The message selector can examine the header and compares an expression which is present in the String. The example for expression comprises of arithmetic operators, relational operators, string literals, and logical operators.     MessageListener:   To process messages asynchronously, we have to use instantiate the MessageListener interface. We have to perform the following steps to process and receive messages asynchronously: Create object which implements MessageListener interface and invoke the onMessage method Use setMessageListener method to register with the session object Use Connection object’s start method to start receiving messages.  
REST APIs and Implementations

REST APIs and Implementations

by Vibrant Publishers on Jan 01, 2022
Introduction Building a RESTful service is specific to every programming language. There are different libraries and implementations of REST in each programming language. In this post, we are going to look into the details of how Java language implements REST and how we can build RESTful applications in Java. Java and REST REST got into the Java world through the release of JavaEE 6. It included the JAX-RS API that is responsible for REST API creation using Java. JAX-RS follows the REST framework and the HATEOAS principle. The current version of JAX-RS is version 2.0. JAX-RS uses annotations for ease of development and deployment of web services. These annotations are part of the java package javax.ws.rs. The basic annotations provided by JAX-RS are: @Path @GET, @POST, @PUT, @DELETE, @HEAD @Produces @Consumes @PathParam JAX-RS Implementations There are many implementations of JAX-RS available in Java. They show minor variations in their ways of defining and using REST APIs. But the basic procedures remain the same in all of them. A few of those implementations are: Jersey: This is the reference implementation of JAX-RS from Sun Microsystems. This is one of the most widely used libraries for building RESTful services in Java. Spring REST: This REST library is part of the Spring web framework. If you are using the spring framework for building your application, making it restful is very easy. It follows an MVC architecture. RESTeasy: RESTeasy is developed by JBOSS and is part of their application server. This implementation has server-side caching and GZIP compression features. Apache CXF: This implementation of JAX-RS specification is by Apache. It is an open source library available for free download and modification. Restlet: This is again an open source implementation of JAX-RS 1.1 specification. It is a light-weight library that supports major media types and all REST specifications. Serialization Frameworks When we implement REST APIs depending on the request and response types, one would always need to use some kind of object serializers. The following are some of the popular serialization frameworks: JAXB It stands for “Java Architecture for XML Binding”. This library helps in the conversion of java objects into an XML data format and vice versa. One can find this API in the javax.xml.bind package. There are many implementations of JAXB in the form of annotations that can be used in java code for conversions. JACKSON Jackson is a JSON processor that is used widely in the Java world for converting Java objects into JSON document format and vice versa. This implementation is part of the Jersey library. Building a RESTful Application As you can see, there are many choices in the Java world for building a RESTful application. The libraries and implementations listed here can be used with any Java framework. It is our choice to go for a particular library based on our use case and technologies that we are adopting. General steps for building any RESTful services would be: Identify resources and build URIs Select or build representations. Identify method semantics. Select response codes. The below diagram shows a simple technology stack for a RESTful application.         Summary After the introduction of REST in JavaEE from version 6 onwards, a lot of implementations and libraries came into existence. The JAX-RS has got strict implementation principles and guidelines for building RESTful API services. As a java web services developer it is important to understand which library to use and how to build RESTful services adhering to the REST principles.  
Rest Resources, HTTP Methods and Status Codes

Rest Resources, HTTP Methods and Status Codes

by Vibrant Publishers on Dec 29, 2021
Introduction REST stands for REpresentational State Transfer. We use REST while doing web services programming to communicate between client and server. There are many concepts involved in the REST world that are required to know for building a RESTful service. We will have a look at some of those building blocks of RESTful service here. ResourcesEverything in REST is a resource. It can be a document, an image, a collection of objects, information stored in the database, etc. REST requires a Unique Resource Identifier (URI) associated with each resource for it to be accessible over the internet. URI is the endpoint of a given resource. The state of each resource is also an important factor while accessing it. The client-side can use the resource in the best possible way when it knows the type and state of the resource. So REST responses will have a machine-readable explanation about the resource. That is a resource representation. It can be the details of the resource, its format, size, etc. REST and HTTPWe saw resources can be of any type, but there are some rules in the REST world while interacting with resources. REST requests and responses use the HTTP protocol. There are specific HTTP message formats that REST clients have to follow while sending a request. The response from the server also will be in a certain message format that a REST client can decode.There are eight different message formats that HTTP has defined. The following are the commonly used four methods. GET: Get a representation of a given resource. DELETE: Destroy the specified resource. POST: Create a new resource, based on the given representation. PUT: Update the state of a given resource with the one described in the given representation. The main aspects of choosing these methods are because of its adherence to the REST specification (RFC 2616). The two main properties in RFC 2616 are safety and idempotency. Safety here means the ability of the client to make requests without altering the state of a given resource. Idempotency means the effect of doing an operation will be the same for single or multiple times. Request Response Structure Now we will see how these methods are used in an HTTP request and response in a RESTful way. A REST client library will create a REST API request in the following format: The API response, in turn, will have the following format:Following is a sample GET request:Method: GET URL: http://api.clothshop.com/shirtsWhich will provide the following response: HTTP/1.1 200 Content-Type: application/json { “id”: 10, “name”: “Lee Shirt”, “color”: “yellow”, “price”: “$30” }HTTP Status CodesWhenever we do a REST API request to the server, the server will return a response with an HTTP status code. Each status code will tell a brief about the response from the server. These status codes will be part of the HTTP header which the client will decode and understand. These are wrapped with HTML code and all HTML /HTML5 browsers are capable of understanding these HTTP codes. The following table summarizes the major status codes and their meaning. HTTP Status Codes Summary 200 OK. Request successful. 201 Resource Created. 204 No content found. 400 Bad Request. 401 Unauthorised. 404 Resource not found. 405 Method not allowed 500 Internal Server Error. Summary There are multiple building blocks in the REST framework that makes web services communication happen over the internet. HTTP protocol is one of the main blocks upon which the entire REST communication is built. While designing a REST API it is important to consider the nature of resources that we are planning to expose as APIs. Also it is important to know various HTTP methods and status codes that will help us in the process of accessing these resources at the client side.