springboot环境上搭建swagger学习

第一步,引入依赖
<!-- Swagger 3 API接口调试工具        -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
第二步:新建配置文件SwaggerConfig
@Configuration
@EnableOpenApi
public class SwaggerConfig {
    /**
     * 是否启用swagger文档,这个可以在application.yml中配置---此次未使用此代码
     */
    /*@Value("${swagger.enable}")
    private boolean enable;*/

    @Bean
    public Docket defaultApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .pathMapping("/")

                 //.enable(enable)//开关代码--不设置开关时,可注释本行代码
                .select()
                // 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                //描述
                .description("项目接口文档")
                .version("1.0")
                .build();
    }
}

第三步,在对应页面中加对应参数:
    a:controller
@Api(tags = "首页接口")//对Controller的描述-----swagger参数
@RestController
@RequestMapping("index")
public class IndexController {
    @ApiOperation( value = "获取今日事件",notes = "获取今日事件")//对接口的描述
    @ApiImplicitParams({//对传参的描述 多个参数就有多个@ApiImplicitParam用,隔开
            @ApiImplicitParam(name = "id", value = "ID", required = false),
            @ApiImplicitParam(name = "age", value = "年龄", required = false)
    })
    @GetMapping("/index")
    public Index todayAlarm(String id,Integer age) {
        Index ret = new Index(id,age);
        return ret;
    }
}
    b:实体类
@ApiModel(description = "实体类")
@Data
public class Index implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("参数1")//参数描述
    private String parme1;

    @ApiModelProperty("参数2")//参数描述
    private Integer parme2;

    public Index(String parme1, Integer parme2){
        this.parme1 = parme1;
        this.parme2 = parme2;
    }
}

这时候重启,可能会重启失败,经过多次测试后,发现是 springboot的版本和swagger3的版本不匹配的问题
将springboot的版本由2.6.4 将为2.5.7版本,再次重启后,就启动正常了,并且前端打开也正常
打开网址http://localhost:自己的端口/swagger-ui/index.html
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version> <!--2.6.4 ->2.5.7-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

目前情况,在启动的时候,ide的console中会可能出现告警情况,如下面一行代码
Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.lang.Void(无法解释数据类型为的隐式参数配置:)
那么避免告警就应该这样处理,在ApiImplicitParam中加上参数的属性dataTypeClass,如下
@ApiOperation( value = "获取今日事件",notes = "获取今日事件")//对接口的描述
    @ApiImplicitParams({//对传参的描述 多个参数就有多个@ApiImplicitParam用,隔开
            @ApiImplicitParam(name = "id", value = "ID", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "age", value = "年龄", required = false, dataTypeClass = Integer.class)
    })
    @GetMapping("/index")
    public Index todayAlarm(String id,Integer age) {
        Index ret = new Index(id,age);
        return ret;
    }

###############
启动和关闭配置
第一步,在application.yml中添加参数
#开启swagger
swagger:
  enable: false
第二步,在SwaggerConfig中加参数获取
/**
     * 是否启用swagger文档,这个可以在application.yml中配置
     */
    @Value("${swagger.enable}")
    private boolean enable;
第三步:在Docket中加上.enable(enable),如下
@Bean
    public Docket defaultApi() {
        System.out.println("enable:"+enable);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .enable(enable)//开关代码
                .select()
                // 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是详细步骤: 1. 创建一个新的 Spring Boot 项目。 2. 在 `pom.xml` 文件中添加对 MyBatis、Swagger 和 Nacos 的依赖: ```xml <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- Nacos --> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.4.0</version> </dependency> ``` 3. 配置 MyBatis 和数据源,在 `application.properties` 文件中添加以下配置: ```properties # MyBatis mybatis.mapper-locations=classpath*:mapper/*.xml # 数据源 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 4. 配置 Swagger,创建一个 `SwaggerConfig` 类: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger API") .description("Swagger API 文档") .version("1.0.0") .build(); } } ``` 5. 配置 Nacos,创建一个 `NacosConfig` 类: ```java @Configuration public class NacosConfig { @Value("${spring.cloud.nacos.discovery.server-addr}") private String serverAddr; @Value("${spring.application.name}") private String appName; @Bean public void init() { Properties properties = new Properties(); properties.put("serverAddr", serverAddr); NacosConfigService configService = NacosFactory.createConfigService(properties); try { String content = configService.getConfig(appName, "DEFAULT_GROUP", 5000); // 解析配置内容 } catch (NacosException e) { e.printStackTrace(); } } } ``` 注意,上面的代码中使用了 `@Value` 注解,需要在 `application.properties` 文件中添加以下配置: ```properties # Nacos spring.cloud.nacos.discovery.server-addr=localhost:8848 spring.cloud.nacos.discovery.namespace= spring.cloud.nacos.config.namespace= spring.cloud.nacos.config.group=DEFAULT_GROUP spring.cloud.nacos.config.file-extension=properties ``` 至此,你已经成功搭建了一个 Spring Boot 项目,并且集成了 MyBatis、Swagger 和 Nacos。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值