Spring Boot integrates Swagger2 to build RESTful API documents

Introducing dependency

Add io.springfox:springfox-swagger2 and io.springfox:springfox-swagger-ui dependencies to pom.xml


Related configuration

Create Swagger2Config configuration class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

public class Swagger2Config {
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apis(RequestHandlerSelectors.basePackage("{Controler Scan path}"))

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .version("{Version number}")

**Note: * * the Controller package path needs to be set in the RequestHandlerSelectors.basePackage parameter, otherwise the generated document cannot scan the interface

Add @ EnableSwagger2 annotation in Application

public class ServiceAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceAdminApplication.class, args);

Using Swagger2

Add the following common Swagger notes to the request interface in the Controller

@RequestMapping(value = "/api/v2/user")
@Api(tags = "user management")
public class UserController() {

	@ApiOperation(value = "Paging query user list")
			@ApiImplicitParam(name = "pageNum", value = "Page number", required = true, dataType = "int", paramType = "path"),
			@ApiImplicitParam(name = "pageSize", value = "The number of pages", required = true, dataType = "int", paramType = "path"),
			@ApiImplicitParam(name = "UserJson", value = "object JSON Character string", required = false, dataTypeClass = String.class, paramType = "json")
	@RequestMapping(value = "page/{pageNum}/{pageSize}", method = RequestMethod.GET)
	public BaseResult page(
		@PathVariable(required = true) int pageNum, 
		@PathVariable(required = true) int pageSize,
		@RequestParam(required = false) String UserJson
		return null;

Instructions for using Swagger annotation

  • @Api: decorate the whole class to describe the function of Controller
  • @ApiOperation: a method, or an interface, that describes a class
  • @ApiImplicitParam: a request parameter
  • @Apiimplicit params: multiple request parameters
  • @ApiParam: single parameter description
  • @ApiModel: receiving parameters with objects
  • @ApiProperty: a field describing an object when receiving parameters with the object
  • @ApiResponse: HTTP response one of the descriptions
  • @ApiResponses: overall description of HTTP response
  • @ApiIgnore: use this annotation to ignore this API
  • @ApiError: information returned when an error occurs

To start the project, visit the Swagger address: http://{ip}:{port}/swagger-ui.html

Keywords: Programming JSON SpringBoot xml Spring

Added by kernelgpf on Sun, 12 Apr 2020 19:28:39 +0300