Swagger笔记

一、导包


    io.springfox
    springfox-swagger2
    2.9.2


    io.springfox
    springfox-swagger-ui
    2.9.2

二、编写配置类

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.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 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggerdemo"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 标题
                .title("Spring Boot中使用Swagger2构建RESTful APIs 的标题")
                // Swagger文档的版本号
                .version("1.0.0")
                // 标题的详细描述
                .description("Spring Boot中使用Swagger2构建RESTful APIs 的详细描述")
                // 友链
                .termsOfServiceUrl("http://www.baidu.com")
                // 更多的描述
                .license("更多的描述")
                // 把更多的描述变成超链接
                .licenseUrl("http://www.baidu.com")
                // 格式化的创作者信息
                .contact(new Contact("蒋劲豪",
                        "http://www.baidu.com",
                        "[email protected]"
                ))
                .build();
    }
}

三、接口文档的访问地址

http://localhost:8080/swagger-ui.html

四、常见问题

SpringBoot和Swagger有版本不兼容问题,如果版本不兼容会出现以下异常:

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 

解决办法:

(一)换兼容的版本

(二)改配置文件

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

五、Controller类的案例

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/userController")
@Api(tags = {"用户接口"})
public class UserController {
    @ApiOperation(value = "新增用户接口", notes = "新增用户接口的详细描述")
    // 对请求参数进行说明
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户编号", dataType = "string",
                    paramType = "query", required = true, defaultValue = "1001"),
            @ApiImplicitParam(name = "name", value = "用户姓名", dataType = "string",
                    paramType = "query", required = true, defaultValue = "tom")
    })
    // 对响应结果进行说明
    @ApiResponses({
            @ApiResponse(code = 200, message = "success", response = User.class)
    })
    @PostMapping("/addUser")
    public String addUser(User user) {
        return "新增用户成功!";
    }
}

六、实体类的案例

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "com.example.swaggerdemo.entity.User", description = "用户的实体类")
public class User {
    @ApiModelProperty(value = "用户编号")
    private String id;
    @ApiModelProperty(value = "用户姓名")
    private String name;
}

你可能感兴趣的:(中间件,java,中间件,Swagger)