springboot项目整合swagger2

一、导入依赖:(目前最新版是3.0,用2.XXX方式可能会不同呦)

        // swagger2
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>

        // swagger-ui
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

        // springfox
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

二、在config=>SwaggerConfig 配置文件里面进行如下配置:

package com.yungongchang.springboot.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

@Configuration
@EnableOpenApi  // 这个注解用来开启swagger2
public class SwaggerConfig {

        /**
         * 配置apiInfo
         * @return
         */
    public ApiInfo apiInfo(){

        //作者信息  可以不用写
        Contact contact = new Contact("名称", "网站名称","你的邮箱");

        return new ApiInfo(
                "title",
                "你的项目description",
                "v1.0",
                "http://localhost:端口号/hello",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }


    // docket
    @Bean
    public Docket docket(Environment environment){

        // 设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");

        // 通过environment.acceptsProfiles 判断是否处于设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("云工厂")
                .select()
                //RequestHandlerSelectors   配置要扫描的方式
                // basePackage 指定扫描的包    basePackage("com.yungongchang.swagger.controller")
                // any();
                // none();  不扫描
                // withClassAnnotation:   扫描类上的注解
                // withMethodAnnotation:  扫描方法上的注解
                .apis(RequestHandlerSelectors. basePackage("com.yungongchang.springboot.controller"))
                // path 过滤什么路径
                // .paths(PathSelectors.ant("/yc/**"))
                .build();
    }

}

 还可以在resource下设置如下配置:(用来指定swagger的环境及端口号)

在application.properties下:

# 指定环境为开发环境。
spring.profiles.active=dev 

 application-dev.properties和application-prod.properties下分别指定端口号:

# dev-- 开发中使用很方便
server.port=8082
# prod 
server.port=8081

三、注解:

1、@ApiOperation:

 作用:用来对接口中具体方法做描述
 位置:作用在方法之上。

效果:

2、@ApiModel:

作用:用来对实体类进行说明
修饰范围:作用在类上

 

效果:

 3、@ApiParam

作用:用来对接口中参数进行说明
修饰范围:作用在方法上

 效果:


总结: 以上就是简单的springboot整合swagger所需用到的一些操作,通常项目上线之后swagger就要被去掉。swagger  3.0的依赖可以访问:http://localhost:端口号/swagger-ui/index.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值