swagger2.0 和swagger3.0 踩坑

最近开始使用swagger 3.0.0来做项目,发现按照网上的教程进行配置,swagger-ui出不来,总是显示404错。经过一番搜索发现,原来是因为swagger升级到3之后,用法上大大简化了。主要的区别在以下两点:

不再需要EnableSwagger2的注解。所以之前的教程其实都不适用于swagger 3.0.0。按照之前的写法去开发,很容易掉坑。
不再需要在dependency里分别引入swagger2的两个包,取而代之的是只需要引入一个"io.springfox:springfox-boot-starter:3.0.0"。和springboot一样简洁。
上述说明其实在swagger官网有说,但是大部分人不看而已,所以给出链接:如何从swagger老版本迁移到3.0.0

swagger2.0 


pom.xml添加依赖

		<!--Swagger版本 2.9.2-->
		<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、创建 Swagger2Configuration.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
​
@Configuration  
@EnableSwagger2
public class Swagger2Configuration {
​
   //api接口包扫描路径
   public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.muyao.galaxy";
​
   public static final String VERSION = "1.0.0";
​
   @Bean
   public Docket createRestApi() {
       return new Docket(DocumentationType.SWAGGER_2)
                   .apiInfo(apiInfo())
                   .select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE)) 
                   .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                   .build();
   }
​
   private ApiInfo apiInfo() {
       return new ApiInfoBuilder()
                   .title("单词计数服务") //设置文档的标题
                   .description("单词计数服务 API 接口文档") // 设置文档的描述
                   .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
                   .termsOfServiceUrl("http://www.baidu.com") // 设置文档的License信息->1.3 License information
                   .build();
   }
}



作者:大鱼炖海棠
链接:https://www.jianshu.com/p/c79f6a14f6c9
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

swagger3.0

maven 

有且只有这一个

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

config

package com.jdwifi.g1335333249.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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

//@Profile("swagger")
//@ComponentScan("com.jdwifi.g1335333249.JdUserLogin.controller")
//public class SwaggerConfig {
//
//    @Bean
//    public Docket createRestApi() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .enable(true)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.jdwifi.g1335333249.JdUserLogin.controller"))
//                .paths(PathSelectors.any())
//                .build();
//    }
//
//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                .title("XXX Rest Server")
//                .description("XXXRest接口")
//                .contact(new Contact("contract", "url", "email"))
//                .version("1.0")
//                .build();
//    }
//}
@Configuration
@EnableOpenApi
public class SwaggerConfig {
    //配置了Swagger的Docket的bean实例
    //enable是否启动swagger,如果为False则Swagger不能在浏览器访问
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors,配置要扫描接口的方式
                //basePackage:指定要扫描的包
                //any():扫描全部
                //none():不扫描
                //.withClassAnnotation():扫描类上的注解
                //.withMethodAnnotation():扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.jdwifi.g1335333249.JdUserLogin.controller"))
                //paths()过滤什么路径
                /*.paths(PathSelectors.ant("/mi/**"))*/
                .build();
    }
    //作者信息
    Contact contact = new Contact("小唐","xiaotangstudio.cn","1738743304@qq.com");

    //配置Swagger 信息 = ApiInfo
    private ApiInfo apiInfo()
    {
        return new ApiInfo("小唐的Api文档",
                "好好看文档",
                "1.0",
                "xiaotangstudio.cn",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }
}

如果报错404 

package com.jdwifi.g1335333249.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.index.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

从现有 2.x 版本迁移 @EnableWebMvc 重要

 上述说明其实在swagger官网有说,但是大部分人不看而已,所以给出链接:如何从swagger老版本迁移到3.0.0

Spring Boot 应用程序

注意:希望得到反馈以使其更好

  1. 删除显式依赖 springfox-swagger2
  2. 删除@EnableSwagger2注释
  3. 添加springfox-boot-starter依赖
  4. Springfox 3.x 移除了对 guava 和其他 3rd 方库的依赖(还不是零依赖!依赖于 spring 插件和用于注释和模型的开放 api 库)所以如果你使用了 guava 谓词/函数,这些将需要转换到 java 8 函数接口
  5. 如果您正在使用 WebMvc 但您还没有使用该@EnableWebMvc注解,请添加此注解。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
npm提供了许多方便的工具和库来生成Swagger 2.0文档。其中一个流行的工具是swagger-jsdoc。通过结合使用JSDoc注释和swagger-jsdoc库,我们可以在JavaScript文件中定义API接口和相应的Swagger文档信息。 首先,在项目中安装npm包swagger-jsdoc。可以使用以下命令: ``` npm install swagger-jsdoc --save ``` 接下来,在项目的入口文件或者需要生成Swagger文档的文件中,引入swagger-jsdoc并配置Swagger文档的相关内容。例如: ```javascript /** * @swagger * definitions: * Pet: * properties: * name: * type: string * age: * type: integer * * @swagger * /pets: * get: * description: 获取所有宠物 * responses: * 200: * description: 成功获取宠物列表 * post: * description: 创建新宠物 * parameters: * - name: pet * description: 宠物对象 * in: body * required: true * schema: * $ref: '#/definitions/Pet' */ // 引入swagger-jsdoc const swaggerJSDoc = require("swagger-jsdoc"); const express = require("express"); const app = express(); // 配置Swagger文档 const swaggerSpec = swaggerJSDoc({ definition: { openapi: "3.0.0", // 使用Swagger版本2.0 info: { title: "宠物商店API文档", version: "1.0.0", description: "这里是宠物商店的API文档" } }, apis: ["./routes/**/*.js"], // 定义API接口的文件路径 }); // 在需要展示Swagger文档的路由上,使用swagger-ui-express库 app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec)); // 其他路由及中间件... // 启动Express服务器... ``` 此外,还可以使用swagger-jsdoc提供的其他功能,例如支持路由的解析、文件导出等。更多的配置和用法可以查看swagger-jsdoc的官方文档。 通过以上步骤,我们就可以使用npm生成Swagger 2.0文档来描述我们的API接口及其相关信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值