Springboot整合Swagger2举例

简介

Swagger2 作为一个规范和完整的框架,可以用于生成、描述、调用和可视化 RESTful 风格的 Web 服务:

  1. 接口文档在线自动生成,文档随接口变动实时更新,节省维护成本

  2. 支持在线接口测试,不依赖第三方工具

 

依赖包

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

 

配置类

package com.gxr.imybatisplus.config;

import springfox.documentation.service.Contact;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger2 配置类
 * 访问地址:http://localhost:18081/swagger-ui.html
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        // springfox.documentation.service.Contact
        Contact contact = new Contact("gongxr", "www.gxr.com", "gxr116@163.com");
        return new ApiInfoBuilder()
                .title("在线API文档")
                .description("这是一个实时更新的在线API文档")
                .contact(contact)   // 联系方式
                .version("1.0.0")  // 版本
                .build();
    }
}

 

接口注解

package com.gxr.imybatisplus.controller;

import com.gxr.imybatisplus.entity.ReturnResult;
import com.gxr.imybatisplus.entity.TSample;
import com.gxr.imybatisplus.service.DataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

@RestController
@RequestMapping("/data")
@Api(tags = "数据接口") // swagger2 注解
public class DataControl {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Autowired
    DataService dataService;

    @ApiOperation(value = "单个查询")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    public TSample getData(@RequestParam @ApiParam(value = "人员编号", required = true, example = "1") String id) {
        logger.info("接口调用");
        TSample sample = dataService.getTSampleById(id);
        System.out.println(sample.toString());
        return sample;
    }


    @ApiOperation(value = "查询所有")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public ReturnResult getAllData() {
        logger.info("接口调用");
        List<TSample> sampleList = dataService.getTSampleList();
        System.out.println(sampleList);
        return new ReturnResult<>(sampleList);
    }

    @ApiOperation(value = "提交字符串")
    @RequestMapping(value = "/postString", method = RequestMethod.POST)
    public ReturnResult postString(@RequestParam @ApiParam(value = "字符串", example = "这是一个字符串") String string) {
        logger.info("接口调用");
        System.out.println(string);
        return new ReturnResult<>("提交成功");
    }


}

 

测试访问

http://localhost:18081/swagger-ui.html

 

 接口调用

 

posted @ 2020-11-17 15:27  星瑞  阅读(181)  评论(0编辑  收藏  举报