Bladex生成Swagger的方法

简介: Bladex生成Swagger的方法

一、在启动类中添加如下代码:(目的是为了打印输出swagger的地址等)

注解:@Slf4j

实现接口:CommandLineRunner

依赖注入:

@Autowired
  private Environment environment;
@Override
  public void run(String... strings) throws Exception {
    try {
      String port = Optional.ofNullable(environment.getProperty("server.port")).orElse("8080");
      log.info("\n------------------------环境信息---------------------------\n\t" +
          "Application '{}' is running! Access URLs:\n\t" +
          "Local  : \thttp://{}:{}\n\t" +
          "Swagger: \thttp://{}:{}/doc.html\n\t" +
          "Profile(s): \t{}\n----------------------------------------------------------",
        environment.getProperty("spring.application.name"),
        InetAddress.getLocalHost().getHostAddress(),
        port,
        InetAddress.getLocalHost().getHostAddress(),
        port,
        Arrays.toString(environment.getActiveProfiles()));
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }

启动类全部代码如下:

/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.launch.BladeApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Optional;
/**
 * 启动器
 *
 * @author Chill
 */
@Slf4j
@EnableScheduling
@SpringBootApplication
public class Application  implements CommandLineRunner {
  @Autowired
  private Environment environment;
  @Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder){
    return builder.build();
  }
  public static void main(String[] args) {
    BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);
  }
  @Override
  public void run(String... strings) throws Exception {
    try {
      String port = Optional.ofNullable(environment.getProperty("server.port")).orElse("8080");
      log.info("\n------------------------环境信息---------------------------\n\t" +
          "Application '{}' is running! Access URLs:\n\t" +
          "Local  : \thttp://{}:{}\n\t" +
          "Swagger: \thttp://{}:{}/doc.html\n\t" +
          "Profile(s): \t{}\n----------------------------------------------------------",
        environment.getProperty("spring.application.name"),
        InetAddress.getLocalHost().getHostAddress(),
        port,
        InetAddress.getLocalHost().getHostAddress(),
        port,
        Arrays.toString(environment.getActiveProfiles()));
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
}

二、找到需要生成Swagger接口文档的控制器类(eg:ActiveCodeController):

1.在控制器类的上面添加注解:

@Api(value = "卡信息", tags = "卡信息接口")

value里面写的是:这个控制器的描述,或者功能

如图所示:

2.在需要生成文档的方法上写注解:

@ApiOperation(value = "详情", notes = "传入cardInfo")

value:接口的功能或者描述

notes:传入参数的描述

全部代码:

/**
   * 详情
   */
  @GetMapping("/detail")
  @ApiOperationSupport(order = 1)
  @ApiOperation(value = "详情", notes = "传入cardInfo")
  public R<CardInfo> detail(CardInfo cardInfo) {
    CardInfo detail = cardInfoService.getOne(Condition.getQueryWrapper(cardInfo));
    return R.data(detail);
  }

或者:

/**
   * 分页 卡信息
   */
  @GetMapping("/list")
  @ApiOperationSupport(order = 2)
  @ApiOperation(value = "分页", notes = "传入cardInfo")
  public R<IPage<CardInfo>> list(CardInfo cardInfo, Query query) {
    IPage<CardInfo> pages = cardInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(cardInfo));
    return R.data(pages);
  }

其他的可以自己补充。

三、配置Swagger的配置类,类路径:BladeX-Boot/src/main/java/org/springblade/common/config/SwaggerConfiguration.java

加入如下代码:

@Bean
  public Docket developerDocket() {
    return docket("开发者中心接口", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.developer"));
  }

【开发者接口】:就是Swagger的接口功能的名称

【.modules.developer】:让Swagger扫描哪个包,指向包就可以。

然后启动服务,启动完成之后,查看控制台打印的Swagger地址。

点击地址就可以查看

相关文章
spring-boot 禁用swagger的方法
在使用spring-boot开发的时候,我们很多时候会使用swagger作为api文档输出。可以在UI界面上看到api的路径,参数等等。 当然,作为开发环境是很方便的,但是上生产环境的时候,我们需要把swagger禁掉。怎么通过配置文件的方法来禁用swagger呢?
2206 0
|
缓存 JSON 安全
从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之五 || Swagger的使用 3.3 JWT权限验证,两种方法
群友反馈: 群里有小伙伴反馈,在Swagger使用的时候报错,无法看到列表,这里我说下如何调试和主要问题: 1、如果遇到问题,这样的:   请在浏览器 =》 F12 ==》 console 控制台 ==》点击错误信息地址 或者直接链接http://localhost:xxxxx/swagger/v1/swagger.
3933 0
|
缓存 Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
本文介绍了在Spring Boot中配置Swagger2的方法。通过创建一个配置类,添加`@Configuration`和`@EnableSwagger2`注解,使用Docket对象定义API文档的详细信息,包括标题、描述、版本和包路径等。配置完成后,访问`localhost:8080/swagger-ui.html`即可查看接口文档。文中还提示了可能因浏览器缓存导致的问题及解决方法。
1273 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
|
JSON Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的使用
本文详细介绍了Swagger2的使用方法,包括在Spring Boot项目中的配置与应用。重点讲解了Swagger2中常用的注解,如实体类上的`@ApiModel`和`@ApiModelProperty`,Controller类上的`@Api`、`@ApiOperation`以及参数上的`@ApiParam`等。通过示例代码展示了如何为实体类和接口添加注解,并在页面上生成在线接口文档,实现接口测试。最后总结了Swagger的优势及其在项目开发中的重要性,提供了课程源代码下载链接供学习参考。
910 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的使用
|
Java Maven 微服务
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的 maven 依赖
在项目中使用Swagger2工具时,需导入Maven依赖。尽管官方最高版本为2.8.0,但其展示效果不够理想且稳定性欠佳。实际开发中常用2.2.2版本,因其稳定且界面友好。以下是围绕2.2.2版本的Maven依赖配置,包括`springfox-swagger2`和`springfox-swagger-ui`两个模块。
577 0
|
前端开发 Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档—— Swagger 简介
第6课介绍了在Spring Boot中集成Swagger2以展示在线接口文档的方法。随着前后端分离架构的发展,API文档成为连接前端与后端开发的重要纽带。然而,代码更新频繁导致文档难以同步维护,Swagger2解决了这一问题。通过Swagger,在线API文档不仅方便了接口调用方查看和测试,还支持开发者实时测试接口数据。本文使用Swagger 2.2.2版本,讲解如何在Spring Boot项目中导入并配置Swagger2工具,从而高效管理接口文档。
425 0
|
数据可视化 Java API
Spring Boot与Swagger的集成
Spring Boot与Swagger的集成
|
Java API 开发者
在Spring Boot中集成Swagger API文档
在Spring Boot中集成Swagger API文档
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
858 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
Java 测试技术 API
详解Swagger:Spring Boot中的API文档生成与测试工具
详解Swagger:Spring Boot中的API文档生成与测试工具
1195 4
下一篇
开通oss服务