SPRINGBOOT学习LESSON2:RESTFULL接口API自动生成

本文介绍如何使用Swagger简化前后端分离模式下接口文档的生成与管理。通过配置springfox-swagger2和springfox-swagger-ui依赖,启用@EnableSwagger2注解,即可实现RESTful API的实时查看和测试。文章还提到了使用特定注解规范接口分类的方法。

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

由于前后端分离的工作模式将面向接口编程,所以接口文档非常重要。使用swagger可是很便捷的生成接口API,并且可以通过接口界面实时查看接口,测试接口。配置也十分简单。

1.配置增加依赖springfox-swagger2,springfox-swagger-ui

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

2.在Application.java即项目启动入口类中使用注解@EnableSwagger2,如

package helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class Application {

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

配置好之后启动项目,然后输入地址:http://localhost/swagger-ui.html
即可访问swagger界面,查看项目 中的所有RESTFULL接口
在这里插入图片描述

编码时使用@DeleteMapping @GetMapping @PutMapping @PostMapping 代替
@RequestMapping 可以对接口进行规范分类。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值