springboot整合swagger demo

 

项目地址链接:

文章底部

前言

 现在的开发基本上都是前后端分离,前后端交互都是通过API文档。有了API文档大家各自开发,互不干扰。

1、传统方式

传统方式是文档设计好之后,分别发给前端和后端人员。这样有个缺点,接口信息一旦变化,文档就需要重新发送给前后端人员。无法做到实时。所以浪费时间和精力。

2、swagger方式

我们的后台应用集成了swagger之后,会自动暴露出我们的接口,而且这个接口形式还是通过restful风格发布的。一旦后端的接口有变化,会立刻显示出来,因此极大地提高了效率。

OK,基本上一句话就可以总结他的好处,那就是后端写的api文档可以通过swagger的形式实时的发布出来,供前端人员查看。

添加依赖
<!--swagger2 依赖-->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.7.0</version>
</dependency>
<!--swagger-ui 依赖 -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.7.0</version>
</dependency>
SwaggerConfig
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2配置类
 * 在与spring boot集成时,放在与Application.java同级的目录下。
 * 通过@Configuration注解,让Spring来加载该类配置。
 * 再通过@EnableSwagger2注解来启用Swagger2。
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {


    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggerdemo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置标题
                .title("标题: xiao pan_接口文档")
                // 描述
                .description("描述: 用于管理xxx项目, xxx模块")
                // 作者信息
                .contact(new Contact("xiao pan","https://blog.csdn.net/weixin_43652507?type=blog","xxx@163.com"))
                .version("版本号: 1.0.0")
                .build();
    }
}

UserController
import com.example.swaggerdemo.domain.User;
import com.example.swaggerdemo.exception.MyException;
import com.example.swaggerdemo.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;


@Api(value = "用户接口", tags = {"用户接口"})
@RestController
public class UserController {


    @ApiOperation("查询用户列表")
    @GetMapping("/list")
    public ResponseResult list(){
        ArrayList<User> list = new ArrayList<>();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setAge(18);
        User user2 = new User();
        user2.setId(2);
        user2.setName("李四");
        user2.setAge(28);
        list.add(user);
        list.add(user2);
        return ResponseResult.createBySuccess(list);
    }

    @ApiOperation(value = "根据用户id查询",notes = "只查询id=1的用户")
    @GetMapping("/selectByUserId")
    public ResponseResult selectByUserId(Integer userId){
        if (userId == null) {
            throw new MyException("用户id不能为空");
        }
        if (userId!=1) {
            throw new MyException("只查询1的用户");
        }
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setAge(18);
        return ResponseResult.createBySuccess(user);
    }

    @ApiOperation(value = "更新用户信息",notes = "请输入id,name,age的值")
    @PostMapping("/update")
    public ResponseResult update(User user){
        if (user == null || user.getId()==null || StringUtils.isEmpty(user.getName()) || user.getAge()==null) {
            throw new MyException("请输入id,name,age的值");
        }
        return ResponseResult.createBySuccess(user);
    }

    @ApiOperation(value = "根据用户id删除")
    @DeleteMapping("/delete")
    public ResponseResult delete(Integer userId){
        if (userId==null) {
            throw new MyException("用户id不能为空");
        }
        return ResponseResult.createBySuccess("操作成功");
    }
}

访问地址:http://项目实际地址/swagger-ui.html

项目地址链接:

https://gitee.com/PanGuanQing/swagger-demo.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yixian123.com

谢谢打赏,祝老板心想事成

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值