REST API Contact and ApiInfo with Swagger

In this Swagger tutorial, I am going to share with you how to add Contact and API information to your RESTful Web Service documentation created with Swagger.

When adding Swagger to our RESTful Web Service, we usually create a new configuration Java Class which is then annotated with @Configuration annotation. Below is an example of a basic Swagger Config class:

Basic Swagger Config Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appsdeveloperblog.app.ws;
 
import java.util.ArrayList;
import java.util.List;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
   @Bean
   public Docket apiDocket() {

       Docket docket =  new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.appsdeveloperblog.app.ws"))
                .paths(PathSelectors.any())
                .build();
       
       return docket;
       
    } 
}

Add Contact and AppInfo to API Documentation

To add contact information to API documentation, add the following code Snippet to the method that returns Docket object.

 Contact contact = new Contact(
         "Sergey Kargopolov",
         "https://www.appsdeveloperblog.com", 
         "[email protected]"
 );

 List<VendorExtension> vendorExtensions = new ArrayList<>();
 
 ApiInfo apiInfo = new ApiInfo(
"Photo app RESTful Web Service documentation", 
"This pages documents Photo app RESTful Web Service endpoints", "1.0",
"http://www.appsdeveloperblof.com/service.html", contact, 
"Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",vendorExtensions);

then, set ApiInfo object to a Docket object, like this:

Docket docket =  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.appsdeveloperblog.app.ws"))
                .paths(PathSelectors.any())
                .build();

Complete Code Example

Below is a complete code example that sets Contact and ApiInfo information to a Docket object.

package com.appsdeveloperblog.app.ws;
 
import java.util.ArrayList;
import java.util.List;
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.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
   @Bean
   public Docket apiDocket() {
       
       Contact contact = new Contact(
               "Sergey Kargopolov",
               "https://www.appsdeveloperblog.com", 
               "[email protected]"
       );
       
       List<VendorExtension> vendorExtensions = new ArrayList<>();
       
       ApiInfo apiInfo = new ApiInfo(
      "Photo app RESTful Web Service documentation", 
      "This pages documents Photo app RESTful Web Service endpoints", "1.0",
      "http://www.appsdeveloperblof.com/service.html", contact, 
      "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",vendorExtensions);
       
       Docket docket =  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.appsdeveloperblog.app.ws"))
                .paths(PathSelectors.any())
                .build();
       
       return docket;
       
    } 
}

Run your Spring Boot application now and open the Swagger UI URL.

http://localhost:8888/mobile-app-ws/swagger-ui.html

If you do not have Swagger UI URL configured, please read this Swagger tutorial to learn How to Add Swagger to a Spring Boot REST API Project.

If all is good you should see your Contact and ApiInfo details set similar to how it is on the picture below:

I hope this short tutorial was of some value to you. If you are interested to learn more about Swagger and enjoy learning by watching video tutorials. Have a look at the below list of video courses. One of them might be exactly what you are looking for.


Leave a Reply

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