当前位置: 首页 > news >正文

Springfox Swagger3从入门案例

首先,在pom.xml中添加依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency>
</dependencies>
 

创建一个配置类SwaggerConfig.java

package org.example.testdoc.config;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;@Configuration
public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("org.example.testdoc.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("Springfox Swagger3 Example").description("This is an example of using Springfox Swagger3 to generate API documentation.").version("1.0").build();}
}
 

创建一个控制器类HelloController.java

package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello")public String hello() {return "Hello, Spring Boot!";}
}
ExampleController类
package org.example.testdoc.controller;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@Api(value = "Example Controller", tags = "example")
@RestController
public class ExampleController {@ApiOperation(value = "Get example data", notes = "This API returns example data.")@GetMapping("/example")public String getExampleData() {return "Hello, World!";}
}

主类:

package org.example.testdoc;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;@SpringBootApplication
@EnableOpenApi
public class TestdocApplication {public static void main(String[] args) {SpringApplication.run(TestdocApplication.class, args);}}

查看结果:访问Swagger UI

http://localhost:8080/swagger-ui/index.html

http://www.lryc.cn/news/291289.html

相关文章:

  • HarmonyOS NEXT 星河版项目案例
  • 房屋租赁系统-java
  • docker环境搭建及其安装常用软件
  • 【Spring连载】使用Spring Data访问Redis(三)----连接模式
  • ppt背景图片怎么设置?让你的演示更加出彩!
  • SQL 关键字参考手册(一)
  • 快速排序|超详细讲解|入门深入学习排序算法
  • 指针+一维整型数组的基本运用 和 指针+一维整型数组的初步学习
  • APP测试基本流程以及APP测试要点总结
  • GPT-4 Vision调试任何应用,即使缺少文本日志 升级Streamlit七
  • ppt形状导入draw.io
  • GoLang和GoLand的安装和配置
  • BGAD文章复现笔记-1
  • 【EI会议推荐】第六届下一代数据驱动网络国际学术会议(NGDN 2024)
  • 聊聊java中的Eureka和Nacos
  • 系统架构设计师-21年-上午试题
  • 数据库MySQL查询设计||给定四个关联表,其定义和数据加载如下:-- 学生表 Student-- 选课表 SC
  • C#使用RabbitMQ-3_发布订阅模式(扇形交换机)
  • 区块链游戏解说:什么是 SecondLive
  • 构建基于Flask的跑腿外卖小程序
  • 【算法】Partitioning the Array(数论)
  • ASP.NET Core 7 Web 使用Session
  • (1)SpringBoot学习——芋道源码
  • 宏景eHR FrCodeAddTreeServlet SQL注入漏洞复现
  • STM32——I2C
  • 笔记本从零安装ubuntu server系统+环境配置
  • SQL 快速参考手册
  • Linux/Windows系统无法git clone解决办法
  • 【算法与数据结构】198、213、337LeetCode打家劫舍I, II, III
  • React、React Router、JSX 简单入门快速上手