SpringBoot 集成Swagger

本文介绍如何使用Swagger2简化API的设计与维护。从引入依赖到配置详解,并通过实例展示如何使用注解来描述接口及其参数。

Swagger简介:

   Design is the foundation of your API development. Swagger makes API design a breeze, with easy-to-use tools for developers, architects, and product owners.

    大致意思是:以Swagger为基础来设计你API,使它变得更简介明了和易于维护;

使用:

    1.引入依赖:

		<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>

2.配置:

    (1)新建配置类,注意与项目的启动类放在同一块:

fb6ff6e39a2ad46ad894152d3f5ff1cbf5d.jpg

    (2)配置swagger的基本信息:

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;

/**
 * Created by linwl on 2018/8/4.
 */
@Configuration
@EnableSwagger2
public class Swagger {
    //指定扫描哪些包
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(swaggeInfo())
                .select()
                //要扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.linwl.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //配置swagger的基本信息
    private ApiInfo swaggeInfo(){
        return new ApiInfoBuilder()
                .title("Spring Boot 测试使用 Swagger2")
                //创始人
                .contact(new Contact("Linwl","https://github.com/swagger-api/swagger-ui/",""))
                //版本
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

(3)编写接口,通过注解对接口进行描述:

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by linwl on 2018/8/4.
 */
@RestController
@RequestMapping("api")
//描述该API类
@Api("SwaggerDemoController相关的接口")
public class SwaggerDemoController {

    //描述该方法函数的用途
    @ApiOperation(value = "根据学生id查询学生信息",notes = "查询数据库中某个学生信息")
    //描述该方法函数需要传递的参数
    @ApiImplicitParam(name = "id",value = "学生Id",paramType = "path",required = true,dataType = "int" )
    @GetMapping("/{id}")
    public String getStudent(@PathVariable int id){
        return "学生Id="+id;
    }

    @ApiOperation(value = "根据学生姓名和学生年龄查询学生信息",notes = "查询数据库中符合条件的学生信息")
    @ApiImplicitParams(
           {@ApiImplicitParam(name = "name",value = "学生姓名",required = false,dataType = "String"),
            @ApiImplicitParam(name="age",value = "学生年龄",required = false,dataType = "int")
           }
    )
    @GetMapping("{name}/{age}")
    public String getStudentByNameAndAge(@PathVariable("name")String name,@PathVariable("age")Integer age){
        return "name:"+name+" age:"+age;
    }

(4)注解说明:

@Api:作用在类上,对该类提供的服务提供说明

@ApiOperation:作用在方法函数上,对该方法函数提供的服务进行说明

@ApiImplicitParam:作用在方法函数上,对该方法函数需要传递的参数进行说明;

@ApiimplicitParams:作用在方法函数上,当该Api需要传递多个参数时使用,封装@ApiImplicitParam

(5)效果图:

5af69b9d5532e26dc806eb70eadc37c0243.jpg

测试:

    (1)点开第一个接口:

71015e3fa38b2ef26d8eb6a67e7e1a4bd53.jpg

(2)点击 Try it out:

d712502412a3103931c2eaf7b722eade9ba.jpg

(3)填写参数,execute。查看结果:

eef7d27dae348c547c56119011c35d3fa59.jpg

 

转载于:https://my.oschina.net/linwl/blog/1922789

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值