springboot2.1.7 integration swagger2.9.2

What is the swagger?

swagger is a framework for defining the API documentation.

Why use swagger?

When the next project development front and rear ends are separated, the interface will become the only link the front and rear end. How to know which interface front-end engineer is doing? Inside there any way? Method requires what parameters? ...... this time we need a simple and comprehensive API documentation, swagger is used to automatically generate API documentation.

How to use swagger?

1. Import dependent pom

Different versions ui interface is different, personal feel more comfortable 2.7.0

<!--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>

2. Configuration write type (replication can be used directly, it is preferable to build a package placed following classes config)

SwaggerConfig类:

@Configuration  //声明这是一个注解类
@EnableSwagger2

public class SwaggerConfig {

    @Bean
    public Docket customDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                 .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors
                .basePackage("com.gl.pin.web.controller"))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("zjk",
                "https://www.cnblogs.com/zjk-main/",
                "[email protected]");
        return new ApiInfoBuilder()
                .title("项目API接口")
                .description("接口描述")
                .contact(contact)
                .version("1.1.0")
                .build();
    }
}

WebMvcConfig categories:

@Configuration 
@EnableWebMvc 
public  class WebMvcConfig the implements WebMvcConfigurer {
     public  void addResourceHandlers (ResourceHandlerRegistry Registry) {
         // solve static resources can not access (optional) 
        / * registry.addResourceHandler ( "/ **") 
                .addResourceLocations ( "the CLASSPATH: / static / "); * / 
        // directly in the browser to access: root directory /swagger-ui.html 
        registry.addResourceHandler (" / Swagger-ui.html " ) 
                .addResourceLocations ( " the CLASSPATH: / META-INF / Resources / " );
         // need to use webjars (includes js, css, etc.) 
        registry.addResourceHandler ( "/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

3. Add comments on each controller, method

@Api applied on the class

@ApiOperation added to the method

Example:

@ResponseBody 
@Controller 
@RequestMapping (value = "/ ADMIN" ) 
@Api (value = "no use, can be unworthy", tags = "admin / zjk ", description = " Administrator Operations" )
 public  class AdminController { 

    @ApiOperation ( httpMethod = "POST", value = "Admin Login", produces = MediaType.TEXT_HTML_VALUE, tags = "admin / zjk", notes = " parameter" ) 
    @PostMapping (value = "/ the Login" )
     public String the Login (adminEntity adminEntity ) {
          return "Login" ; 
    } 
}

4. access to the root directory /swagger-ui.html

In this page to see a description of the controller class, the class described which method, method parameters, the return value and the like. swagger also provides an interface test function is similar to the postman.

Guess you like

Origin www.cnblogs.com/zjk-main/p/11530541.html