集成swager遇到的一些问题

今天给接口都增加了swagger说明,因为写文档还是比较费时间的。遇到了一些问题;

自己本地启动,swager页面可以正常使用,但是打包部署到服务器上却报找不到静态资源。

解决方法:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMVCConfig extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
}

引入swager核心包的时候,说guava太老了,有些方法没有,解决方法就是单独把guava再引入一个更新的版本

 <!-- Swagger核心包 start -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>15.0</version>
        </dependency>
        <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>
        <!-- Swagger核心包 end -->

通过component把链接hbase的方法改造了一下,支持多环境使用

@Component
public class HBaseConnecter {
    private static String quorum;

    @Value("${hbase.zookeeper.quorum}")
    public void setquorum(String qur){
        quorum = qur;
    }

    public Configuration conf(){
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum",quorum);
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        return conf;
    }
}

swagger对中文支持可能不太友好;tags写中文,点击接口会无法展开调试页。

@Api(tags = "redis tools")

后记:

总之最后弄好了还是蛮开心的~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Spring Boot可以很方便地集成Swagger,只需要添加相应的依赖和配置即可。 1. 添加Swagger依赖 在pom.xml文件中添加以下依赖: ``` <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> ``` 2. 配置Swagger 在Spring Boot的配置类中添加Swagger的配置: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } } ``` 其中,`RequestHandlerSelectors.basePackage`指定扫描的包路径,`PathSelectors.any()`表示所有路径都可以访问Swagger。 3. 启动应用 启动应用后,在浏览器中输入`http://localhost:808/swagger-ui.html`即可访问Swagger UI界面,查看API文档。 以上就是Spring Boot集成Swagger的简单步骤。 ### 回答2: Swagger是一个API文档自动生成工具,它可以让我们非常方便地生成API文档,同时还能够提供可视化的API测试功能。Spring Boot是一个非常流行的Java Web框架,由于其自带的约定优于配置的特点,使得它的开发效率非常高,同时Spring Boot也支持很多插件的集成。本文主要介绍如何在Spring Boot项目中集成Swagger。 一、添加Swagger依赖 在Spring Boot 2.0版本之后,Swagger 2.x已经被弃用,取而代之的是Springfox。因此,我们需要添加相应的依赖: ```xml <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> ``` 以上两个依赖分别是Swagger的核心库和UI库。 二、配置Swagger 在Spring Boot项目中,我们需要进行Swagger的配置,在application.properties中添加以下配置: ``` #将Swagger的文档设置为开启 springfox.documentation.swagger.v2.enabled=true #设置Swagger扫描的包 swagger.scan.basePackage=com.example.demo.controller #设置Swagger文档的标题 swagger.title=Spring Boot整合Swagger学习笔记 #设置Swagger文档的描述 swagger.description=Spring Boot整合Swagger学习笔记描述 #设置联系人 swagger.contact.name=Swagger swagger.contact.url=http://www.baidu.com swagger.contact.email=你的邮箱 #设置版本号 swagger.version=0.0.1-SNAPSHOT ``` 三、编写接口 在接下来的步骤中,我们需要编写一些简单的API接口来测试Swagger的功能。 ```java @RestController @RequestMapping("/api") @Api(tags = "测试API") public class TestController { @GetMapping("/hello") @ApiOperation(value = "Hello World", notes = "输出Hello World字符串") public String hello() { return "Hello World"; } } ``` 以上代码是一个最简单的示例,其中使用@RestController注解声明类为控制器,@RequestMapping表示该类中的所有API访问的基础路径为“/api”,@Api用于指定一个API分组(swagger中将多个API聚合成一个API文档),@ApiOperation表示具体的一个API接口。 四、访问Swagger 当以上步骤完成之后,我们可以启动Spring Boot项目并在浏览器中访问http://localhost:8080/swagger-ui.html,页面如下所示: ![image-20210911101424552](https://i.imgur.com/8Z6qTn2.png) 可以看到,Swagger UI界面非常友好,我们可以在页面中查看到我们编写的所有API,还提供了测试功能以及查看API状态码的功能,非常方便。 五、总结 Spring Boot整合Swagger非常简单,只需要引入相关依赖以及添加相应的配置即可。通过Swagger我们可以快速生成API文档以及提供API可视化测试的功能,非常方便。 ### 回答3: Springboot 是一种非常流行的 Java Web 开发框架,并且随着 RESTful API 的普及,Swagger 也成为了一个非常流行的 API 文档生成工具。将 Swagger 集成到 Springboot 中,可以为 API 的开发和维护提供非常方便的文档支持。 集成 Swagger 的第一步是添加 Maven 依赖。在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.x.x</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.x.x</version> </dependency> ``` 然后,我们需要编写 Swagger 的配置文件。这个配置文件是一个 Java 类,其中包含一些注解,用于配置 Swagger 的 API 文档生成规则。 @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .build(); } } 在以上示例代码中,我们使用 @Configuration 注解标注这个类为 Springboot 的配置类。@EnableSwagger2 注解用于启用 Swagger。在构建 Docket 对象时,我们可以通过配置要扫描的 API 包的路径来确定我们要生成文档的代码。 最后,我们需要添加一个 API 上的注解,以指示 Swagger 将其包含在生成的 API 文档中。在 Springboot 中,我们可以使用 @ApiOperation 和 @ApiParam 这两个注解来实现这个目标。例如: @RestController @RequestMapping("/users") @Api(tags = "用户管理") public class UserController { @ApiOperation(value = "获取用户列表", notes = "获取所有用户信息") @GetMapping("") public List<User> getUsers() { // ... } @ApiOperation(value = "添加用户", notes = "添加新用户") @PostMapping("") public User addUser(@ApiParam(value = "用户对象", required = true) @RequestBody User user) { // ... } // ... } 在这个示例代码中,我们为包含在文档中的 API 添加了 @ApiOperation 和 @ApiParam 注解。@ApiOperation 注解用于描述 API 的功能和用途。@ApiParam 注解用于描述 API 参数的名称、类型、限制等信息,以及是否是必需的。 到此为止,我们已经完成了 Springboot 集成 Swagger 的全部过程。当我们启动 Springboot 应用程序并打开Swagger UI(例如:http://localhost:8080/swagger-ui.html)时,我们应该会看到生成的 API 文档。这些文档将非常有用,不仅可以用于开发过程中的文档说明,还可以用于 API 客户端的自动生成。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

シミコ

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值