SpringBoot与Swagger2整合-11

本文介绍如何在SpringBoot项目中集成Swagger2,通过添加相应依赖和注解,实现RESTful API的自动生成与展示。具体步骤包括在启动类上添加@EnableSwagger2Doc注解,配置pom.xml引入swagger-spring-boot-starter和swagger-bootstrap-ui依赖,以及在控制器中使用@Api和@ApiOperation等注解来描述接口功能。

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

首先要在启动类上添加注解@EnableSwagger2Doc
在pom.xml文件中添加坐标:
<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.9.0.RELEASE</version>
    <!--排除自带 UI 依赖-->
    <exclusions>
        <exclusion>
            <artifactId>springfox-swagger-ui</artifactId>
            <groupId>io.springfox</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.2</version>
</dependency>
控制层代码
@RestController
@Api(tags = "用户的控制层")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("user/{id}")
    @ApiOperation(value = "根据id进行查询",notes = "备注")
    @ApiImplicitParam(value = "主键",type = "path",name = "id",required = true,dataType = "int")
    public User findById(@PathVariable("id") Integer id){
        return userService.findById(id);
    }

    @PostMapping("addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "新增成功";
    }
}

访问页面展示效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值