Interface Specification swagger

1. Why do we need

  • Interface testers to test interface by interface description - black box
  • To use the interface front-end developers via interface description.

2. Write Interface doc document

  • Direct sorting out all interfaces each have access address (access method), parameters and return values.
  • May be able to make direct reception by the back-end code generation developers or testers can read documents generated swagger

3. To achieve

  • Introducing jar package swagger needed
<!-- swagger引入包-->
	<properties>
        <!--swagger对应的版本-->
        <springfox.version>2.4.0</springfox.version>
    </properties>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>
  • Write SwaggerConfig class
package cn.wxy.crm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//springboot  Configuration 相当于写一个applicationContext.xml
@Configuration
@EnableWebMvc  //开关配置 开启webmvc
@EnableSwagger2 //开启swagger配置
@ComponentScan(basePackages= "cn.wxy.crm.web.controller") //扫描controller
public class SwaggerConfig {

    //相当于 <bean id="api" class=""Docket/>
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.wxy.crm.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    
    //生成内容 接口的信息
    private ApiInfo apiInfo(){
         @SuppressWarnings("deprecation")
         ApiInfo info=new ApiInfo(
                 "crm的接口文档",
                 "王哈哈",
                 "1.0",
                 "www.wxy.cn",
                 "王哈哈",
                 "1",
                 "www.wxy.cn");
         return info;
    }
}
  • Scanning swagger package file applicationContext-mvc.xml
<!-- 把swagger交给spring-->
    <context:component-scan base-package="cn.wxy.crm.config"></context:component-scan>

4. Run

http://localhost/swagger-ui.html

5. show results

Here Insert Picture Description

Published 33 original articles · won praise 0 · Views 417

Guess you like

Origin blog.csdn.net/weixin_45737653/article/details/104502179