White road springboot of (two), integrated swagger

0- Introduction

  Now the project development, are basically separate front and rear end, back-end focus on the API interface development, you need to write and maintain API interface documentation. If you are still using Word to write the interface documentation, you're out, and this time, Dangdang Dangdang ~ magic weapon swagger grand appearance! Swagger automatically generated API documentation according to the project, can help us better to write and maintain API documentation. Spring boot swagger that integration is a must, really easy to use, with who knows who.

1-springboot integrated swagger

  swagger swagger integration is very simple, just a few steps:

1-1, add the maven-dependent:

    <!-- 4、集成swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

1-2, added swagger configuration

package anson.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.builders.ApiInfoBuilder;
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;


/**
 * @description: Swagger配置类
 * @author: anson
 * @Date: 2019/9/3 22:52
 * @version: 1.0
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig
{
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //swagger要扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("anson.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot Swagger 测试")
                .description("Springboot 整合Swagger2")
                .termsOfServiceUrl("localhost:8090")
                .contact(new Contact("Swagger测试","localhost:8090/swagger-ui.html","[email protected]"))
                .version("1.0")
                .build();
    }
}

1-3, add annotations related swagger controller in

package anson.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: user接口
 * @author: anson
 * @Date: 2019/9/3 21:33
 * @version: 1.0
 */

@Api(value = "用户接口")
@RestController
@RequestMapping ( "/ User" )
 public  class the User 
{ 
    / * 
     * @author Anson 
     * @Description methods have parameters 
     * @date 2019/9/3 23:29 
     * @Param [ID] 
     * @return java.lang.String 
    * / 
    @ApiOperation (value = "get users", notes = "id query based on user information" ) 
    @ApiImplicitParam (name = "id", value = "user id", required = to true , dataType = "String") // API parameter 
    @RequestMapping (value = "/ getUserById", Method = RequestMethod.
    GET)
    public String getUser(String id)
    {
        return ("Hello anson-" + ID);
    }

    /*
     * @Author anson 
     * @Description non-parametric method 
     * @date 2019/9/3 23:29 
     * @Param [] 
     * @return java.lang.String 
    * / 
    @ApiOperation (value = "get user", notes = "Get user information" ) 
    @RequestMapping (value = "/ the getUser", Method = RequestMethod.GET)
     public String the getUser () 
    { 
        return "Anson the Hello!" ; 
    } 
}

Is completed, it's that simple; after running the line input configuration URL you can see the API documentation, API test can be run directly on top

 

 Well, the end of this section, see the following section

Guess you like

Origin www.cnblogs.com/yanghj/p/11456590.html