SpringBoot-Swapper的使用

SpringBoot-Swapper的使用

1、导入maven依赖

2.x版本导入两个依赖

访问路径为:http://127.0.0.1:8080/swagger-ui.html

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

3.x版本导入一个依赖

访问路径为:http://127.0.0.1:8080/swagger-ui/index.html

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

2、创建配置类

config>SwapperConfig.java

@Configuration
@EnableSwagger2  //开启Swagger2
public class SwapperConfig {

    //配置Swapper的Docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
//        获取到当前是生产环境还是发布环境,从而来进行自动的改变swapper配置
        Profiles dev = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(dev);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                .select()
//                RequestHandlerSelectors:
//                    basePackage 指定要扫描的包
//                    any() 扫描全部
//                    none() 不扫描
//                    withClassAnnotation 扫描类上的注解,参数是一个注解的放射对象,annocation.class
//                    withMethodAnnotation 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.tuzhi.controller"))
                .paths(PathSelectors.any())//过滤路径
                .build();
    }

//    配置Swapper信息
    public ApiInfo apiInfo() {
        Contact contact = new Contact("吕竟", "127.0.0.1/hello", "542918096@qq.com");
        return new ApiInfo(
                "兔子的Api",
                "这是一个HelloSwapper",
                "2.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>()

        );
    }

}

3、注解

  1. @ApiModel(“xx实体类”):加在实体类上,给类注释

  2. @ApiModelProperty(“用户名xx”):加在属性上,给属性注释

  3. @ApiOperation(“xxx接口”):加在控制方法上,给方法注释

  4. @Api(tag = {“xxx接口”}):加在控制类上,给控制类注释

  5. @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "nameHello", value = "你要Hello的内容",dataType = "String")
    }) //加在控制类方法上,给方法都参数添加注释
    
  6. @ApiModel("用户")
    @Data
    public class User {
        @ApiModelProperty("用户名")
        private String username;
        @ApiModelProperty("密码")
        private String password;
    }
    
    @RestController
    @Api(tags = {"用户接口"})
    public class HelloController {
    
        @GetMapping("/hello/{nameHello}/{author}")
        @ApiOperation("hello")
    
        @ApiImplicitParams(value = {
                @ApiImplicitParam(name = "nameHello", value = "你要Hello的内容",dataType = "String"),
                @ApiImplicitParam(name = "author", value = "作者名",dataType = "int")
        })
        public String hello(String nameHello,int author) {
            return "hello" + nameHello + author;
        }
    
        @PostMapping("/add")
        @ApiOperation("添加用户")
        public User add(User user) {
    
            return user;
        }
    
    }
    
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!要将Spring Boot与Swagger集成,你可以按照以下步骤操作: 1. 添加Swagger依赖:在你的Spring Boot项目的`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2. 创建Swagger配置类:在你的Spring Boot项目中创建一个配置类,例如`SwaggerConfig.java`,并添加以下内容: ```java import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { } ``` 3. 配置Swagger信息:在上述配置类中,你可以添加更多的Swagger配置信息,例如API文档的标题、描述等。以下是一个示例: ```java import org.springframework.context.annotation.Bean; 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 SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API Documentation") .description("API Documentation for my Spring Boot project") .version("1.0.0") .build(); } } ``` 在上面的示例中,你需要将`com.example.controller`替换为你的控制器类所在的包路径。 4. 启动应用程序并访问Swagger UI:在你的Spring Boot应用程序启动后,你可以通过访问`http://localhost:8080/swagger-ui.html`来查看生成的API文档。在Swagger UI中,你可以浏览和测试你的API。 这就是将Spring Boot与Swagger集成的基本步骤。你可以根据需要进一步自定义Swagger配置。希望能对你有所帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值