springboot集成Swagger3.0版本以及一些常见配置

一、创建springboot项目

二、添加swagger相关依赖

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

三、在启动类上加上@EnableOpenApi注解

@SpringBootApplication
@EnableOpenApi
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

四、启动,输入http://localhost:8081/swagger-ui/index.html

出现此页面表示成功
在这里插入图片描述

五、可自定义一个Swagger配置类

可以把上面启动类的@EnableOpenApi删除

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;



/**
 * @author LTL
 * @date 2021-06-28 16:53
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {
    //配置swagger的Docket的Bean实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo());
    }

    //配置swaagger信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("ltl","","17700*****@qq.com");

        return new ApiInfo("ltl的标题Api Documentation",
                "ltl的描述Api Documentation",
                "版本信息1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }

}

运行结果如下:
在这里插入图片描述

可指定扫描方式

    //配置swagger的Docket的Bean实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                //.enable(false)
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式,可点击该源码查看扫描方式
                //basePackage:指定扫描包;any()扫描全部;none():不扫描;
                .apis(RequestHandlerSelectors.basePackage("com.ltl.controller"))
                .build();
    }

enable(false)表示关闭swagger,可以自定义哪些环境需要开启服务
在这里插入图片描述

可配置API文档分组,指定自己的swagger配置,实现多个Docket即可

   //配置swagger的Docket的Bean实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("田思雨")
                //.enable(false)
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式,可点击该源码查看扫描方式
                //basePackage:指定扫描包;any()扫描全部;none():不扫描;
                .apis(RequestHandlerSelectors.basePackage("com.ltl.controller"))
                .build();
    }
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.OAS_30).groupName("田思雨1号");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.OAS_30).groupName("田思雨2号");
    }

效果如下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

六、在实体类和controller加注解

@Api(tags="")用于Controller
@ApiOperation用于Controller内的方法
@ApiParam用于Controller内的方法的参数解释
@ApiModel标识类名
@ApiModelProperty用于解释属性名称

@ApiModel("用户实体类")
public class User {

    @ApiModelProperty("用户id")
    private int id;
    @ApiModelProperty("用户名")
    private  String username;
    @ApiModelProperty("用户密码")
    private  String password;
    @ApiModelProperty("用户邮箱")
    private  String email;
}
    //用户登录
    @ApiOperation("用户登录控制")
    @PostMapping("/login")
    public String login(@ApiParam("邮箱") @RequestParam("email") String email,
                        @RequestParam("password")String password,
                        HttpSession session, Map<String,Object> map){
                     }

效果如下
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 要在Spring Boot项目中集成Swagger 3.0,您需要以下步骤: 1. 在pom.xml文件中添加Swagger的依赖 2. 在启动类中添加@EnableSwagger2注解 3. 创建一个Docket Bean,并配置Swagger信息 4. 在控制器类和API方法上添加Swagger注解,来生成API文档 5. 启动项目并访问http://localhost:端口/swagger-ui.html查看API文档 具体的实现方法可以参考Swagger的官方文档和示例代码。 ### 回答2: Swagger是一款流行的API文档工具,Swagger 3.0 是当前最新版本,提供了全新的设计风格和功能。Spring Boot 是一款非常优秀且流行的Java框架,能够轻松集成和搭建不同的应用。在构建RESTful web服务时,我们可能需要使用Swagger来进行API的文档化和测试。由于Spring Boot的优秀特性和灵活性,它可以很方便地与Swagger进行集成。 Spring Boot的Swagger集成,主要依赖于两个库,它们分别是Swagger UI和Swagger Core。Swagger UI库是提供了一个交互式的Web页面,可以让我们很方便地查看和测试API。Swagger Core库则是提供了注解和解析功能,可以实现和扫描类、方法和参数的注解,最后生成API文档。 那么接下来,我将简单介绍在Spring Boot中如何集成Swagger 3.0,并说明如何使用它来生成我们需要的API文档。 首先,在你的Spring Boot项目中引入Swagger UI和Swagger Core的依赖库,建议使用最新版本,这样可以获得更好的性能和支持: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0-SNAPSHOT</version> </dependency> ``` 然后,在入口类(通常是Applicaton类)上添加注解@EnableSwagger2。例如: ```java @SpringBootApplication @EnableSwagger2 public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 接下来,我们需要配置Swagger的基础信息,例如API文档的标题、描述和版本号等。在Spring Boot中,可以在application.yml或者application.properties配置文件中添加以下内容: ```yaml springfox.documentation.swagger-ui.enabled=true springfox.documentation.swagger-ui.title=My Awesome API Doc springfox.documentation.swagger-ui.description=Description for My Awesome API Doc springfox.documentation.swagger-ui.version=1.0 springfox.documentation.swagger-ui.terms-of-service-url=http://www.mycompany.com/terms springfox.documentation.swagger-ui.contact.name=API Support springfox.documentation.swagger-ui.contact.url=http://www.mycompany.com/support springfox.documentation.swagger-ui.contact.email=support@mycompany.com springfox.documentation.swagger-ui.license.name=Apache 2.0 springfox.documentation.swagger-ui.license.url=http://www.apache.org/licenses/LICENSE-2.0.html ``` 之后,我们需要在Controller层的类或者方法上加上Swagger的注解来描述API。例如: ```java @RestController public class MyController { @ApiOperation(value = "Get username", notes = "Return username by user id") @ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 500, message = "Internal server error") }) @GetMapping("/users/{id}") public String getUser(@PathVariable(name = "id") Long id) { return "user-" + id; } } ``` 以上示例中,用到的注解包括:@ApiOperation,用来定义操作方法的描述信息;@ApiResponses,用来定义操作方法的返回结果;@GetMapping,用来定义Controller类的Get请求。 最后,我们需要访问Swagger UI的页面,在浏览器地址栏输入http://localhost:8080/swagger-ui/index.html,即可查看到API文档的Web界面。在UI界面上,我们可以看到API的基本信息、请求参数、响应结果等内容。同时,在UI界面上,也可以直接对API进行测试并调试。特别是,在操作全局变量或者数据库的情况下,使用Swagger可帮助我们大大提高开发效率和减少错误。 ### 回答3: Swagger是一个开源的API框架,可以生成API文档和测试客户端,并提供了可视化的UI让用户直接测试API。而Spring Boot是一个非常流行的Java框架,它简化了Java web应用的开发。针对这两个框架的结合,可以实现在Spring Boot中集成Swagger,从而给Java web应用带来更加便捷的API文档和测试方式。本文详细介绍了如何在Spring Boot中集成Swagger3.0。 1、引入Swagger3.0依赖 首先,我们需要在pom.xml文件中引入Swagger3.0的依赖包: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2、配置Swagger3.0 在Spring Boot的配置文件application.yml中,我们需要添加Swagger3.0的配置信息。具体而言,需要添加以下内容: ```yaml springfox: documentation: swagger-ui: enabled: true # 是否启用Swagger UI springfox: documentation: enabled: true # 是否启用Swagger文档 swagger-ui: enabled: true # 是否启用Swagger UI title: API文档 # Swagger UI的标题 description: API接口文档 # Swagger UI的描述信息 version: 1.0.0 # Swagger UI的版本信息 base-package: com.example.demo.controller # 扫描API文档所在的controller包 ``` 3、编写API文档 在Java开发中,通常通过注解的方式来提供API文档。Swagger3.0也不例外,它提供了一些注解,用于描述API的基本信息、请求参数、响应参数等。举个例子,下面是一个简单的UserController,其中涉及了Swagger3.0的注解: ```java @RestController @RequestMapping("/user") public class UserController { @ApiOperation(value = "获取用户详细信息", notes = "根据用户的ID来获取用户详细信息") @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path") @GetMapping("/{id}") public User getUser(@PathVariable int id) { // 根据id获取用户信息 return user; } } ``` 其中,@ApiOperation注解用于描述API接口的基本信息,包括接口名称、接口说明等;@ApiImplicitParam注解则用于描述请求参数的基本信息,包括参数名称、参数类型、是否必填等。 4、访问Swagger UI 在完成上述步骤之后,我们就可以访问Swagger UI了。默认情况下,Swagger UI的URL为http://localhost:8080/swagger-ui/index.html,其中8080是Spring Boot应用的端口号,可以根据实际情况进行调整。在Swagger UI中,可以看到所有API接口的详细信息,包括请求参数、响应参数等。 总结 通过上述步骤,我们就可以在Spring Boot中集成Swagger3.0,方便的生成API文档并提供可视化的UI来测试API。在真实的开发环境中,我们可以根据实际需求来调整Swagger3.0的配置,以便更好的满足我们的开发需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

田思雨》

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值