SpringBoot整合Swagger

Swagger 是什么可以干什么?

Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
Swagger 的目标是对 REST API 定义一个标准且和语言无关的接口,可以让人和计算机拥有无须访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过 Swagger 进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger 消除了调用服务时可能会有的猜测。
Swagger 的优势
支持 API 自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需要自己手动编写接口文档了,对程序员来说非常方便,可以节约写文档的时间去学习新技术。
提供 Web 页面在线测试 API:光有文档还不够,Swagger 生成的文档还支持在线测试。参数和格式都定好了,直接在界面上输入参数对应的值即可在线测试接口。(摘抄来自http://c.biancheng.net/view/5532.html)

开始配置

pom配置文件

<!--swagger  依赖-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("indi.tsz.swagger.controller"))   							 //扫描接口的包
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder().title("接口文档标题")
                        .description("接口文档详细信息")
                        .version("1.0")
                        .contact(new Contact("谭绍宗","http://tszdemon.online","2440392137@qq.com"))
                        .license("执照")
                        .licenseUrl("https://www.baidu.com/")
                        .build());
    }
}


访问:http://localhost:8080/swagger-ui.html
效果图
在这里插入图片描述

controller注解

package indi.tsz.swagger.controller;

import indi.tsz.swagger.entiy.JsonData;
import indi.tsz.swagger.entiy.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
@Api(tags = "用户服务接口相关接口")  //用来指定接口的描述文字   作用在类上
public class UserController {

    @GetMapping("find_all")
    @ApiOperation(value = "查询所有用户的接口",
            notes = "<span style='color:red;'>描述:</span>&nbsp;&nbsp;用来查询所以的用户接口的详细信息的描述")
    public JsonData findAll() {

        return new JsonData(200, "查询成功");
    }


    @PostMapping("save")
    @ApiOperation(value = "保存用户信息接口1",
            notes = "<span style='color:red;'>描述:</span>&nbsp;&nbsp;保存用户信息接口的详细信息的描述")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "String", defaultValue = "小神"),
            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", defaultValue = "123456xxx")
    })
    public JsonData save1(String name, String password) {

        return new JsonData(200, "保存成功");
    }


    @PostMapping("save2/{name}/{password}")
    @ApiOperation(value = "保存用户信息接口2",
            notes = "<span style='color:red;'>描述:</span>&nbsp;&nbsp;保存用户信息接口的详细信息的描述")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "String", defaultValue = "小神"),
            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", defaultValue = "123456xxx")
    })
    public JsonData save(@PathVariable("name") String name, @PathVariable("password") String password) {
        Map<String, String> map = new HashMap<>();

        map.put("name", name);
        map.put("password", password);

        return new JsonData(200, "保存成功", map);
    }


    @PostMapping("save3")
    @ApiOperation(value = "保存用户信息接口3",
            notes = "<span style='color:red;'>描述:</span>&nbsp;&nbsp;保存用户信息接口的详细信息的描述")
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有授权"),
            @ApiResponse(code = 200, message = "成功")
    })
    public JsonData save(@RequestBody User user) {
        Map<String, String> map = new HashMap<>();

        map.put("name", user.getName());
        map.put("password", user.getPassword());

        return new JsonData(200, "保存成功", map);
    }
}


访问:http://localhost:8080/swagger-ui.html
效果图

  1. 在这里插入图片描述
  2. 在这里插入图片描述
  3. 在这里插入图片描述
  4. 在这里插入图片描述
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值