springboot2 集成swaggerui

1.引入jar包

      <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

2.编写SwaggerConfig类

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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;

/**
 * swagger相关配置类
 */
@EnableSwagger2
@Configuration
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("cn.plca.test")).paths(PathSelectors.any()).build();// cn为扫描的包路径
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")// 页面标题
                .contact(new Contact("sw", "url", ""))// 作者
                .version("1.0")
                .description("描述").build();
    }
}

3.配置application.yml文件 作用: @ConditionalOnProperty(name = "swagger.enable", havingValue = "true") 会扫描到application.yml文件中的swagger.enable的值,从而决定是否开启swaggerui  用于 我们在生产环境的时候关闭 开发环境的时候开启

swagger.enable: true

 

4.编写congroller

@RequestMapping("test")
@Controller
@Api(description = "类的描述")
public class TestController {

    @PostMapping(value = "/list")
    @ApiOperation(value = "方法描述", notes = "测试使用")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
    @ApiImplicitParam(name = "test",value = "测试纸",required = false,dataType = "string",paramType = "body")
    })
    @ApiParam(name = "test",value = "测试参数")
    @ResponseBody
    public Result loginApp(@Validated({Group1.class}) @RequestBody PlaceVo placeVo, @RequestBody String test) throws Exception {
        return Result.success(null);
    }
    
}

1.@ApiImplicitParam 参数解释 name为参数的名称,value 为对参数的描述 dataType为 参数类型 paramType 参数请求的位置

2.当我们使用对象来接收参数使用对象来接收

@Builder
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
public class LoginPcUser implements  User{
    @ApiModelProperty(value = "用户名", required = true)
    @NotBlank(groups = Group1.class,message = "用户名不能为空")
    private String name;
    @ApiModelProperty(value = "密码", required = true)
    @NotBlank(groups = Group1.class,message = "密码不能为空")
    private String password;
}

@ApiModelProperty属性就可以描述了

3.返回值描述 在我们返回的 code,message,data 对data用泛型描述

springboot2 集成swaggerui

 

 这样返回的对象中有@ApiModelProperty的都会描述

 

上一篇:4.Springboot2.x集成 RabbitMQ的几种常见用法


下一篇:Springboot2.x集成Redis哨兵模式