Swagger nanny level teaching

Swagger nanny level teaching

Swagger Profile

Swagger is a standardized and complete framework for generating, description, and visualization calls for RESTful Web services. The overall objective is to make the client and the file system as a server at the same rate updates. Methods, parameters and models are tightly integrated into the file server-side code that allows the API to always be in sync.

Quick Start

1. Create a new project springboot

2. Import maven dependent (swagger2 and swagger UI)

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

3. Start project, visit http: // localhost: port /swagger-ui.html

4. Create a SwaggerConfig.java

package com.littlepage.config;
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 配置swagger bean实例
     * @return
     */
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }
    
    /**
     * 配置swagger信息
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfo("steve yu's API document", 
                "for quick start swagger", "1.0", 
                "https://www.cnblogs.com/littlepage/", 
                new Contact("steve yu", "https://www.cnblogs.com/littlepage/", "[email protected]"),
                "Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0",new ArrayList<>());
    }
}

Start page

The scan interface configuration (modified Bean)

@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2).
            apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.littlepage.controller")).build();
}

6. Configuration (Scan of interface switch) in the environment

@Bean
public Docket docket(Environment environment){
    Profiles profiles=Profiles.of("dev","test");//假设有dev和test环境变量
    boolean flag=environment.acceptsProfiles(profiles);
    return new Docket(DocuemtatuionType.SWAGGER_2).enable(flag);//如果环境配置了,则生效,否则不生效
}

Packet 7.API document.

new Docket().group(String s)
//这个String s可以规定分组

8. A single API samples (Reference https://www.jianshu.com/p/349e130e40d5)

#####Controller代码
@Override
    @ApiOperation(value = "post请求调用示例", notes = "invokePost说明", httpMethod = "POST")
    public FFResponseModel<DemoOutputDto> invokePost(@ApiParam(name="传入对象",value="传入json格式",required=true) @RequestBody @Valid DemoDto input) {
        log.info("/testPost is called. input=" + input.toString());
        return new FFResponseModel(Errcode.SUCCESS_CODE, Errcode.SUCCESS_MSG);
    }


#####接口请求入参对象   
@Data
@ApiModel(value="演示类",description="请求参数类" )
public class DemoDto implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotNull
    @ApiModelProperty(value = "defaultStr",example="mockStrValue")
    private String strDemo;

    @NotNull
    @ApiModelProperty(example="1234343523",required = true)
    private Long longNum;

    @NotNull
    @ApiModelProperty(example="111111.111")
    private Double doubleNum;

    @NotNull
    @ApiModelProperty(example="2018-12-04T13:46:56.711Z")
    private Date date;
    
}

#####接口请求出参公共类
@ApiModel(value="基础返回类",description="基础返回类")
public class FFResponseModel<T> implements Serializable {

    private static final long serialVersionUID = -2215304260629038881L;
    // 状态码
    @ApiModelProperty(example="成功")
    private String code;
    // 业务提示语
    @ApiModelProperty(example="000000")
    private String msg;
    // 数据对象
    private T data;

...
}

#####接口请求出参实际数据对象
@Data
public class DemoOutputDto {

    private String res;

    @NotNull
    @ApiModelProperty(value = "defaultOutputStr",example="mockOutputStrValue")
    private String outputStrDemo;

    @NotNull
    @ApiModelProperty(example="6666666",required = true)
    private Long outputLongNum;

    @NotNull
    @ApiModelProperty(example="88888.888")
    private Double outputDoubleNum;

    @NotNull
    @ApiModelProperty(example="2018-12-12T11:11:11.111Z")
    private Date outputDate;
    
}

Guess you like

Origin www.cnblogs.com/littlepage/p/11614718.html