Springfox Swagger3从入门案例

简介: 本文通过一个简单的案例介绍了如何在Spring Boot项目中使用Springfox Swagger3来生成和配置API文档,包括添加依赖、创建配置类、编写控制器类以及访问Swagger UI界面。

首先,在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 {
    @Bean
    public 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

目录
相关文章
|
22小时前
|
Java API Spring
Springfox Swagger2从入门到精通
本文详细介绍了如何使用Springfox Swagger2在Spring Boot项目中生成API文档,包括引入依赖、配置Swagger2、启用Swagger2、编写API文档注释、访问Swagger UI以及常用注解分析和高级配置。
7 0
Springfox Swagger2从入门到精通
支付系统----微信支付16----创建案例项目-引入Swagger
支付系统----微信支付16----创建案例项目-引入Swagger
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程
|
11月前
|
前端开发 Java API
深入剖析 Swagger enum:实际案例详解
enum 是 Swagger 规范中用来定义枚举类型的一种方式。它允许开发者在 API 文档中明确列出该接口的参数、返回值或请求体中可接受的枚举值。通过使用 Swagger enum,开发者可以更清晰地描述 API 的输入和输出,提高 API 文档的可读性和可维护性。
|
XML JSON 人工智能
swagger入门
swagger入门
|
缓存 Java Spring
Springfox swagger2 自定义配置ApiInfo
Springfox swagger2 源码解析
769 0
|
JSON 开发框架 前端开发
【小家Spring】借助Springfox整合SpringBoot和Swagger(API接口神器)
【小家Spring】借助Springfox整合SpringBoot和Swagger(API接口神器)
【小家Spring】借助Springfox整合SpringBoot和Swagger(API接口神器)
|
数据可视化 大数据
Swagger的使用(第一个案例)
Swagger的使用(第一个案例)
359 0
Swagger的使用(第一个案例)
|
前端开发 Java API
Spring Boot 2.x基础教程:使用SpringFox 3生成Swagger文档
Spring Boot 2.x基础教程:使用SpringFox 3生成Swagger文档
302 0
Spring Boot 2.x基础教程:使用SpringFox 3生成Swagger文档
|
Java API Spring
MP实战系列(十)之SpringMVC集成SpringFox+Swagger2
该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例。 不过建议最好采用的是JDK8+Spring+MyBatis Plus+SpringMVC,因为本示例就是基于这个。
1705 0