SpringBoot集成接口文档swagger

1、引入依赖
<!-- swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
2、编写配置文件
package com.newcapec.swaggerdemo.configuration;

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

/**
 * swagger配置类
 * 访问地址:http://localhost:8077/swagger-ui.html#/
 * 作者: 彭赛赛
 * 创建时间:2019/12/27 11:29
 */
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.newcapec.swaggerdemo.controller"))//可修改的内容
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("我是标题:彭赛赛") //可修改的内容
                .description("我是描述:彭赛赛管理平台")//可修改的内容
                .version("1.0")//可修改的内容
                .build();
    }
}
3、编写一个controller
package com.newcapec.swaggerdemo.controller;
import java.util.ArrayList;
import	java.util.List;

import com.newcapec.swaggerdemo.configuration.MyConfig;
import com.newcapec.swaggerdemo.model.Person;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * hello
 * 作者: 彭赛赛
 * 创建时间:2019/12/26 13:59
 */
@RestController
@Api(tags = "测试模块")
public class Hello {

    @Autowired
    private MyConfig myConfig;

    @ApiOperation(value = "我是hello方法的value",notes ="我是hello方法的notes",response = String.class)
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        System.out.println(myConfig.getUsername() + ":" + myConfig.getPassword() + ":" + myConfig.getAge());
        return "hello";
    }

    @ApiOperation(value = "getPersons的value" , notes = "getPersons的notes", response = List.class)
    @RequestMapping(value = "/getpersons",method = RequestMethod.GET)
    public List<Person> getPersons(){
        ArrayList<Person> list = new ArrayList<>();
        list.add(new Person(1,"深圳"));
        list.add(new Person(2,"朝阳"));
        list.add(new Person(3,"赛赛"));
        return list;
    }

}
4、启动项目,输入地址(在配置文件的注释中)即可查看接口文档

在这里插入图片描述

5、相关注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
6、踩坑记录
  • 方法名一定要指定请求方式,否则会为一个方法生成所有的请求方式
  • 接口一定是RESTfull风格,否则测试接口的时候不会起作用
  • 在主启动类上添加@EnableSwagger2注解
  • 未完待续……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值