您的位置:首页 > 编程语言 > Java开发

简化说明:springboot整合swagger2

2018-10-24 18:34 816 查看

springboot整合swagger2

swagger2说明:swagger2是一个自动生成webApi的框架,只需要简单的整合到springboot框架就可以自动、同步生成webAPI

整合步骤:

    1)在springboot中添加依赖包

<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)编写swagger的配置类。就两个方法。主要写api的一些基本信息

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tydic.project.com"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("RESTful APIs")
                .description("swagger2 自动生成Api")
                .termsOfServiceUrl("https://blog.csdn.net/qq_24380635")
                .contact("@jony")
                .version("1.0")
                .build();
    }
}

如果在配置类和入口类添加@EnableSwagger2都表示开启swagger2

 

   3)在controller类,或你想要提供给前端的类上添加swagger2的注解就可以了,使用注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

 1、@Api()用于类; 表示标识这个类是swagger的资源
 2、@ApiOperation()用于方法; 表示一个http请求的操作
 3、@ApiParam()用于方法,参数,字段说明; 表示对参数的添加元数据(说明或是否必填等)
 4、@ApiModel()用于类 ,表示对类进行说明,用于参数用实体类接收
 5、@ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改
 6、@ApiIgnore()用于类,方法,方法参数 。表示这个方法或者类被忽略 
 7、@ApiImplicitParam() 用于方法。表示单独的请求参数 
 8、@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam

  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: