Swagger配置

先引入依赖

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger2</artifactId>

            <version>2.7.0</version>

        </dependency>

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger-ui</artifactId>

            <version>2.7.0</version>

        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.6</version>
        </dependency>
    </dependencies>

在application启动类加注解@EnableSwagger2

再配置SwaggerConfig文件

package com.zed.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启swagger
public class SwaggerConfig {
    @Bean
    public Docket docketA(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @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("wzzz")
                //enable 是否启用swagger 如果为false 则swaager不能在浏览器中访问
                .enable(flag)
                .select()
                //RequestHandlerSelectors 配置要扫描接口的方式
                //basePackage :指定要扫描的包
                //any:扫描全部
                //none :不扫描
                //withClassAnnotation :扫描类上的注解。参数是一个注解的反射对象
                //withMethodAnnotation :扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.zed.demo.controller"))
                //paths 过滤什么路径 只扫描过滤的接口
                //.paths(PathSelectors.ant("/wz"))
                .build();


    }

    //作者信息
    Contact contact = new Contact("wz", "https://i.csdn.net/#/uc/profile", "wz@163.com");

    private ApiInfo apiInfo(){
        return  new ApiInfo("wz的swaggerApi",
                "sagger测试",
                "v1.0",
                "#",
                contact,
                "Apache 2.0",
                "https://www.apache.org/licenses/LICENSE-2.0.html",
                new ArrayList<>());

    }
}

其中判断生产环境可分别配置application.properties application-dev.properties application-pro.properties
application.properties内

spring.profiles.active=dev

application-dev.properties内

server.port=8081

application.properties内

server.port=8082

现在这种配置只有http://localhost:8081/swagger-ui.html#/会显示8082环境就不会显示

而返回多个Docket可以配置多个Api文档分组 当前配置显示页面如下
会有多个Api分组显示
编写Controller

package com.zed.demo.controller;
import com.zed.demo.pojo.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "Swagger控制类",description = "Swagger控制类")
@RestController
@RequestMapping("/test")
public class SwaggerController {
    //ApiOperation方法上
    @ApiOperation("Swagger-test控制方法")
    @GetMapping
    public String test(){

        return "test";
    }
    //只要接口中返回值存在实体类,就会被扫描到Swagger中
    @ApiOperation("Swagger-user控制方法")
    @PostMapping("/user")
    public User user(@ApiParam("用户") User user){

        return user;
    }
}

实体类

package com.zed.demo.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
//@Api(注释)
@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    private String name;
    @ApiModelProperty("密码")
    private String pws;

    public User() {
    }

    public User(String name, String pws) {
        this.name = name;
        this.pws = pws;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPws() {
        return pws;
    }

    public void setPws(String pws) {
        this.pws = pws;
    }
}

效果图
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值