Skip to main content

Simple rest API using Spring boot + maven + dev tools + swagger

What we’ll build

In this article we are going to create simple rest API by using spring boot and maven.

Dev tools used for auto build and deploy application. During development, application build and deployed on server automatically.

Similarly swagger is used for auto generate rest API’s documentations. As a result no need to create manually rest API’s documentations.

We will discuss above all in-depth in subsequent sections. So lets start first setup project and develop simple rest API.

Technologies Used

List of technologies used in this application.

  • Spring Tool Suite 4
  • JDK 8
  • Maven 3
  • Spring-boot 2.1.6.RELEASE

Project Directory Structure

Project directory structure

1- pom.xml

Add the given below dependencies in project.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- devtools -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>

		<!-- swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>
		
		<!-- swagger UI -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2- Setup Swagger for auto generation of rest API’s documentation

Suppose we created multiple rest API’s. Also front-end team wants to consume these API’s. So in this situation we can utilize swagger.

Swagger used for auto generate rest API documentation. It will automatically generate rest API’s documentation. So that front-end team know about API’s purpose, URL, request method (get/post/put/etc.), request parameters, sample response etc.

Setup swagger in application is very simple. We need to add even more given below 2 dependencies.

    
   <!-- swagger -->
   <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>

    <!-- swagger UI -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>

After adding given above all maven dependencies into .pom file. Also create one swagger configuration file as given below.

SwaggerConfig.java

package com.example.demo;

import java.util.Collections;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * The Class SwaggerConfig.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

	/**
	 * Api.
	 *
	 * @return the docket
	 */
	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.example"))
				.paths(PathSelectors.any()).build().apiInfo(apiInfo());
	}

	/**
	 * Api info.
	 *
	 * @return the api info
	 */
	private ApiInfo apiInfo() {
		return new ApiInfo("First Spring boot demo API'S",
				"API related to simple demo to kick start and start working with spring boot rest API develoment.",
				"Spring Boot Simple Demo", "Terms of service",
				new Contact("Rupendra Sharma", "https://www.tecmentor.in", "rupendra1817@gmail.com"), "License of API",
				"API license URL", Collections.emptyList());
	}
}

3- Verify swagger setup

If you follow the given above all steps. As a result swagger will successfully setup in your application. Finally it’s time to verify the swagger setup.
So use http://localhost:8080/swagger-ui.html URL to access swagger GUI. If setup correctly, then given below screen will display.

Verify swagger setup

4- Controller

SimpleController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * The Class SimpleController.
 */
@RestController
public class SimpleController {

	/**
	 * Gets the welcome msg.
	 *
	 * @return the welcome msg
	 */
	@GetMapping(value = "/welcome-msg")
	public String getWelcomeMsg() {
		return "Welcome in Spring Boot world.";
	}

	/**
	 * Gets the sum result.
	 *
	 * @param firstNum
	 *            the first num
	 * @param secondNum
	 *            the second num
	 * @return the sum result
	 */
	@GetMapping(value = "/sum")
	public Long getSumResult(Long firstNum, Long secondNum) {
		return firstNum + secondNum;
	}
}

5- Invoke created API’s using swagger UI

Finally we have created 2 rest API’s. So now its time to open the swagger GUI. By using given below URL to invoke and test the create API’s.
http://localhost:8080/swagger-ui.html

Invoke created API’s using swagger UI

1- To invoke /sum rest API follow the given below steps:

To invoke /sum rest API follow the given below steps

2- To invoke /welcome-msg rest API follow the given below steps:

To invoke /welcome-msg rest API follow the given below steps

Summary

We have created 2 rest API’s using spring boot + maven + dev tools and swagger. By using swagger GUI, invokes and test the created API’s.

Finally we have done. That above all from my end.
Feel free to ask furthermore question or queries related to this post.


Download source code

Download Source Code: demo.zip

One thought to “Simple rest API using Spring boot + maven + dev tools + swagger”

  1. Helpful info. Fortunate me I found your site unintentionally, and
    I’m stunned why this twist of fate did not came
    about in advance! I bookmarked it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Top