Designing REST API - What is Code First Approach?


Image


Designing Great REST API is important to have great microservices. Code First approach focuses on generating the contract from code. Is it the best possible approach?

You will learn

  • What is Code First approach of designing REST API?
  • What are advantages of Code First approach?
  • What are disadvantages of Code First approach?
  • When do you adopt Code First approach?

Free Courses - Learn in 10 Steps

REST API

This is the fourth article in a series of articles on REST APIs:

What Is Code First?

Whenever you develop service, such as a REST API or a SOAP API, there are two approaches you could choose from:

  • Code First, and generate the contract out of the code
  • Contract First, and develop the code based on the contract

Let’s start with a quick example for Code First

Spring Boot Code First REST API Example

We are developing a RESTful web service, by making use of the Spring Boot Framework to generate the API. For example, in the retrieveAllUsers() API, we expose the "/users" URI and return all the user resources by calling a service method. when we hit this URL, we get these users returned:

image info

In a similar manner, there are other service methods defied, each with their own URI. What we are doing with this example is taking the code, and generating documentation from it. That documentation lists out how a user can use the service. For this, we use a documentation format called Swagger:

image info

Swagger enables us to generate documentation from the code. For example, the following is what Swagger generates for a request to retrieve all users:

image info

It lists out the kind of response message we get, and the response status accompanying it as well. You can even call that service from Swagger, leading to:

image info

You can also send a POST request to "/users":

image info

Swagger tells us how to structure the request message, and specify the individual field formats inside. It will also tell you the type of response you get back, along with the response code.

What Swagger generates out of the code, is called a contract.

Advantages Of Code First

The advantages of this approach are mainly:

  • Low-effort contracts: Generating the contract does not require any additional effort. It is just a by-product of the service development, as it can be automatically generated from the code
  • Contract, code in-sync: As the contract is generated from the code, the two always in sync with each other

Disadvantages Of Code-First

The disadvantages of this approach are as follows:

No Parallel Development

The service producer and the service consumers cannot be developed in parallel. First the service needs to be developed, then the contract is generated, and only after that, consumer code can be written to adhere to the contract. Without understanding the contract, a consumer cannot be developed.

No Target For Teams

As the contract cannot be known before the service is developed, there is no target for the various stakeholders in development. Hence, there is every chance that directions go wayward, and unnecessary changes are made, leading to wastage of effort.

No Cross-Platform Compatibility

In some older frameworks, it is not that easy to generate the contract from the code. As a result, it is quite common for the contracts generated not being compatible across platforms.

Do check out our video on this:

image info

Summary

In this article, we explored the code first approach of building REST API. While code first approach is efficient from a developer perspective, it has significant challenges when it comes to developing the service provider and consumer together.

Best Selling Udemy Courses

Image
Image Image Image Image Image Image Image Image Image

Join 450,000 Learners and 30+ Amazing Courses

350,000 Learners are learning everyday with our Best Selling Courses : Spring Boot Microservices, Spring, Spring Boot, Web Services, Hibernate, Full Stack React, Full Stack Angular, Python, Spring Interview Guide, Java Interview, Java Functional Programming, AWS, Docker, Kubernetes, PCF, AWS Fargate and Azure


Do not know where to start your learning journey? Check out our amazing learning paths:
Learning Path 01 - Spring and Spring Boot Web Applications and API Developer,
Learning Path 02 - Full Stack Developer with Spring Boot, React & Angular,
Learning Path 03 - Cloud Microservices Developer with Docker and Kubernetes,
Learning Path 04 - Learn Cloud with Spring Boot, AWS, Azure and PCF and
Learning Path 05 - Learn AWS with Microservices, Docker and Kubernetes


Subscribe

FREE COURSES



Related Posts

Writing Integration Tests for Rest Services with Spring Boot

Setting up a basic REST Service with Spring Boot is a cake walk. We will go one step further and add great integration tests!

Integrating Spring Boot and Spring JDBC with H2 and Starter JDBC

Learn using Spring Boot Starter JDBC to connect Spring Boot to H2 (in memory database) using Spring JDBC. You will create a simple project with Spring Boot. You will add code to the project to connect to a database using Spring JDBC. You will learn to implement the basic CRUD methods.

JUnit Tutorial for Beginners in 5 Steps

JUnit Tutorial for Beginners in 5 Steps. Setting up a basic JUnit example and understanding the basics of junit.

JPA and Hibernate Tutorial For Beginners - 10 Steps with Spring Boot and H2

JPA and Hibernate in 10 Steps with H2 - Setting up a basic project example with Spring Boot and in memory database H2. Its a cake walk.

Spring Boot Tutorial For Beginners in 10 Steps

Introduction to Spring Boot in 10 Steps. Learn the basics of Spring Boot setting up a basic project example with Spring Boot.

Spring Framework Tutorial for Beginners - Your First 10 Steps

Learn the basics of Spring Framework setting up a very simple example.

JPA and Hibernate Tutorial using Spring Boot Data JPA

Complete journey starting from JDBC to JPA to Spring Data JPA using an example with Spring Boot Data JPA starter project. We use Hibernate as the JPA Implementation.

Creating a Web Application with Spring Boot with JSP

Setting up a basic web application with Spring Boot is a cake walk. We will create a simple web application using Spring Initializr and add JSP features to it.

What is Spring Boot Auto Configuration?

Auto Configuration is the most important feature in Spring Boot. In this tutorial, we will learn important concepts about Auto Configuration with a couple of examples.

Unit Testing Rest Services with Spring Boot and JUnit

Setting up a Basic REST Service with Spring Boot is a cake walk. We will go one step further and add great unit tests to our RESTful Service.


Search