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>
AI 代码解读

创建一个配置类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();
    }
}
AI 代码解读

创建一个控制器类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!";
    }
}
AI 代码解读

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!";
    }
}
AI 代码解读

主类:

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);
    }

}
AI 代码解读

查看结果:访问Swagger UI

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

目录
打赏
0
0
0
0
136
分享
相关文章
|
6月前
|
SpringBootWeb篇-入门了解Swagger的具体使用
通过上述步骤,您可以在 Spring Boot 项目中快速集成和使用 Swagger。Swagger 提供了简洁的配置和强大的功能,使得 API 文档的生成和测试变得非常方便。通过 Swagger 的注解,开发者可以清晰地描述 API 的功能,提高文档的可读性和可维护性。通过访问 Swagger UI,您可以直观地查看和测试 API,极大地提升开发效率。
118 7
|
9月前
|
Springfox Swagger2从入门到精通
本文详细介绍了如何使用Springfox Swagger2在Spring Boot项目中生成API文档,包括引入依赖、配置Swagger2、启用Swagger2、编写API文档注释、访问Swagger UI以及常用注解分析和高级配置。
448 0
Springfox Swagger2从入门到精通
深入剖析 Swagger enum:实际案例详解
enum 是 Swagger 规范中用来定义枚举类型的一种方式。它允许开发者在 API 文档中明确列出该接口的参数、返回值或请求体中可接受的枚举值。通过使用 Swagger enum,开发者可以更清晰地描述 API 的输入和输出,提高 API 文档的可读性和可维护性。
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
177 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等