Swagger的基本用法

本文档介绍了如何在Spring Boot应用中集成Swagger-UI,通过添加相关依赖、配置Swagger并编写Controller,实现RESTful API的文档化。重点在于Swagger配置类的编写和Controller的@Api注解,最后展示了在http://localhost:8083/swagger-ui.html访问API文档的步骤。请注意版本兼容性,避免出现访问404的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swagger-UI的使用
1.导入依赖:

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

2.编写配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Bean
    public Docket  docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mongodb.swagger.controller"))//配置扫描的包
                .build();
    }


    private ApiInfo  apiInfo(){
        Contact contact=new Contact("张在成","https://www.cnblogs.com/developer_chan/p/9231761.html","1204133120@qq.com");//基本信息
        return new ApiInfo("张在成的SwaggerUi文档","坚持就是胜利","v1.0","https://www.cnblogs.com/developer_chan/p/9231761.html",contact,
                "Apache 2.0","http://www.apache.org./licenses/LICENSE-2.0",new ArrayList<>());
    }

}

3.编写controller:

@RestController
@Api("订单处理类")
@RequestMapping("/order")
public class OrderController {

    /**
     * 获取订单详情
     * @return
     */
    @GetMapping("/user")
    public  String  getOrderInfo(){
        return  "张杰";
    }
}

4.http://localhost:8083/swagger-ui.html进行访问
在这里插入图片描述
头部是基本信息展示
在这里插入图片描述
下部是order-controller是我们的类里的自定义控制器

希望对各位小伙伴有帮助,记得注意版本问题,3.0.0版本的在访问web界面会报404,具体什么原因后来再更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值