Posts

Showing posts with the label REST

Introduction to Swagger

Recently, I got an opportunity to understand a bit about Swagger and how it works. So in this article, I will be explaining what swagger is, how it works. This article is for those readers who have heard about Swagger but wish to know about it further. Swagger is a tool that developers can use to document REST services. It is based on the Open API specification. Challenges in documenting a REST application SOAP web services use a WSDL file for documenting the web service. However, when it comes to REST services, there are no established standards for documentation.  So developers need to create documents manually. This method is tedious and error prone. Open API Specification OPEN API is a specification that developers can use to document REST services. It helps in documenting the following: Available HTTP methods(GET, POST, PUT, etc) supported by each REST method, Input/outputs that the REST service accepts/produce Authentication information Open API specification files can be written...

JAX-RS Exception Handling explained with code samples

There are several ways to perform exception handling in a JAX-RS application. In this article, I will be explaining some of the JAX-RS exception handling mechanisms. Using an ExceptionMapper One way to perform exception handling in a JAX-RS application is using a custom exception mapper. The following code demonstrates a custom exception mapper: @Providerpublic class MyExceptionMapper implements ExceptionMapper<MyException>{@Overridepublic Response toResponse(MyException ex) {return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();}}   This is an Exception Mapper for a custom exception called MyException. It has the @Provider annotation. The MyExceptionMapper class implements the ExceptionMapper interface and overrides the toResponse method. In this method, you can place the correct HTTP status code. In this case, the code uses the HTTP status code for Internal Server Error i.e. code 500. So this code will be sent to the client when MyException occurs. Throwing ...

JAX-RS vs Spring REST - Differences

In this article, I will be covering the differences between JAX-RS/Jersey and Spring REST. What is REST? REST stands for Representational State Transfer.  It is an architectural style that can be used for web services. A REST server exposes functionality as REST endpoints which are simply URLs. A REST client simply uses the services exposed by the REST server by accessing the corresponding URL. Edit What is JAX-RS JAX-RS stands for Java API for RESTful Web Services (JAX-RS). It is the standard Java API for developing RESTful web services. The latest version of JAX-RS is JAX-RS 2.0.  It is part of Java EE 7, so it does not need to be included separately. It is just a specification, it does not provide an implementation. Jersey is the reference implementation of JAX-RS specification. There are other implementations of JAX-RS like RESTEasy, etc   What is Spring REST The Spring framework also provides REST support. The Spring MVC module can be used to develop a REST service....

Important JAX-RS annotations explained

In this article, I will be explaining some important annotations in JAX-RS What is JAX-RS JAX-RS is a Java API that you can use for building a REST service. JAX-RS stands for Java API for RESTful Web Services (JAX-RS). It is part of the Java EE since Java 6. The latest version of JAX-RS is JAX-RS 2.0 and is part of Java EE 7. JAX-RS is just an API; it has some annotations that you can use to build a REST application.  JAX-RS does not include an implementation. Jersey framework is the reference implementation of JAX-RS specification and implements JAX-RS 2.0. There are other implementations of JAX-RS like RESTEasy, etc. Important JAX-RS annotations @Get/@Post/@Put/@Delete/@Head You need to specify these annotations on a JAX-RS method. These annotations indicate the HTTP method that the JAX-RS method maps to. So when a client request comes in, JAX-RS matches the HTTP method in the client request to the method that has the appropriate annotation. If there is no annotation on a method, JAX...

Spring REST client using RestTemplate in Eclipse and Maven

Image
In this article, I will be demonstrating how you can create a Spring REST client using RestTemplate. In order to see how you can create a simple Spring REST service, you can refer to this blog post. What is Spring RESTTemplate? Spring provides a class called RestTemplate . This is the starting point in creating a REST client application.  It uses the URI and the HTTP method to invoke in order to connect to a REST service. It has several methods that allow you to invoke the HTTP methods on the specified URI Edit Project Creation Step 1 – Create a Maven Project in Eclipse . You can refer to this blog post. You will get a project as follows:   Step 2 – Add dependencies to POM file. Add the following to your pom.xml file: <properties> <spring.framework>5.1.3.RELEASE</spring.framework> <spring.web>5.1.3.RELEASE</spring.web> </properties> <dependencies> <dependency> <groupId>org.springframewo...

How to create a JAX-RS Jersey client application for consuming a REST service

Image
In this blog post, I will be demonstrating how you can create a JAX-RS client application using Jersey JAX-RS client implementation. What is JAX-RS and Jersey Client API? JAX-RS is the Java specification for Restful web services. It is just a specification, it does not provide an implementation. Jersey is the reference implementation of JAX-RS specification. There are other implementations of JAX-RS like RESTEasy, etc  The first release of JAX-RS was JAX-RS 1.0. It did not provide a client API. So vendors like Jersey, RESTEasy provided their own client APIs. JAX-RS 2.0 (which is the latest release of JAX-RS) provides a client API. The latest release of Jersey (2.25.1) implements this client API. So in addition to its proprietary client API, Jersey also implements the JAX-RS client API.  While implementing a REST client that uses Jersey, it is better to use the JAX-RS specific client implementation and not Jersey’s client API. So even if the JAX-RS vendor is changed from Jersey to ...

Create a REST service in Java using JAX-RS and Eclipse

Image
In this article, I will be demonstrating creating a REST service in Java using JAX-RS and Eclipse. I will be using Maven and Eclipse. Create a Project and And Maven Support Step 1 – Follow the steps in  this  article to create a web project in Eclipse with Maven Support. You should see a project as follows: Step 2 – Add Maven dependencies for JAX-RS and Jersey as follows: <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-bundle</artifactId> <version>1.19.4</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20170516</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> ...

Creating a Spring REST application in Maven and Eclipse

Image
In this article, I will be demonstrating how to create a REST application via Spring using Maven and Eclipse. Create a Project and And Maven Support Step 1 – Follow the steps in this article to create a web project in Eclipse with Maven Support. You should see a project as follows: Edit Step 2 – Add the Maven Dependencies for Spring to the pom file as follows: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.3.RELEASE</version> ...

Controller Vs RestController annotation in Spring

In this blog post, I will be explaining the Controller and RestController Spring annotations and also their differences. MVC V/s REST Before diving into the Controller and RestController annotations, it is important to understand the difference between a normal MVC web application and a REST application. An MVC Web application returns HTML which is rendered in a browser and displayed to end users.  A REST application, on the other hand, returns an XML or JSON response. Edit Controller Annotation You can use the Controller annotation to designate a Controller class in an MVC application. Normally in such scenarios, the code maps each method in the controller to a URL specified via the RequestMapping annotation. When the UI requests a particular URL, the code invokes the appropriate method. This executes the business logic and then redirects the user to the appropriate JSP page. Consider the following code: @Controllerpublic class HelloWorldController { @RequestMapping("...

How to create Spring Boot REST Service and Test it via Postman

Image
In this blog post, I will be explaining how you can create a Spring Boot REST service.  If you would like to see how to create a basic HelloWorld Spring Boot application refer to this blog post. Edit Project Creation and Setup Step 1 – Create a new Maven Project (Refer to this blog post)   Step 2 – Add the following Spring Boot dependencies to the POM file and save. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version></parent><dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> Edit Writing Code Step 3 – Create Person class as follows package com.learnjava.model;public class Person { private int id; private String name; private int age; public int getId()...

JSON Syntax and Benefits explained with code samples

In this blog post, I will be explaining JSON Syntax and benefits. We will be taking a look at how it compares to XML as well. What is JSON JSON stands for JavaScript Object Notation. It is a lightweight transfer protocol. It is based on the JavaScript language. JSON is used primarily in web services to transfer data.  Data sent via JSON is concise and easy to read. Why was JSON required? Prior to JSON, XML was used to transfer data between web services. The downside of using XML is that it is very verbose. For example, suppose you have a Person class as follows:   public class Person { private String firstName; private String lastName; private int age; //Getter and setter methods } And suppose we create a Person list with 3 Person objects as follows: List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Mickey","Mouse",35)); personList.add(new Person("Donald","Duck",30)); personList.add(new Person("Peppa...

What is REST API

Image
In this blog post, I will be explaining what a REST API is. What is REST REST stands for Representational State Transfer. It is a design pattern for web services. Web services that follow this design pattern are known as RESTful web services. There are 2 main components in a REST API. Client The person or application that uses the services is the client. The client can be a web application, mobile application or even an individual developer. Server The server provides some functionality. The client application makes an HTTP call to the server. Any HTTP method (i.e. GET, POST, PUT, DELETE) can be used. The server then acts on the request by the client. So if the client sends an HTTP GET request, the server returns the requested information. If the client sends an HTTP POST request, the server performs the necessary operation with the data sent by the client. How REST works The server exposes functionality as REST endpoints which are simply URLs. The client application needs to access th...