Swagger简单使用

Swagger

作用: ·1、实时更新后端接口信息(swagger-ui.html, 该页面用来协同前端同事进行接口对接)

             2、swagger-ui.html在线测试接口

使用:

1、导入依赖(尽量不用3.0以上版本,3.0以上改动过大, 直接打开swagger-ui.html会显示404):

        <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、controller例子

testController.java
Swagger简单使用
package com.ll.swaggerdemo.controller;

import com.ll.swaggerdemo.pojo.HelloBean;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Api(tags = "用户控制器")
public class testController {

    @ApiOperation("hello 方法")
    @RequestMapping(value = {"hello"}, method = RequestMethod.POST)
    @ResponseBody
    public String hello(@ApiParam("用户") HelloBean ban) {
        return new HelloBean("userName", "oassword").toString();
    }
}
View Code

 

3、swagger配置类

SwaggerCongfig.java
Swagger简单使用
package com.ll.swaggerdemo.config;

import com.ll.swaggerdemo.pojo.HelloBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerCongfig {

    // Docket对象1
    @Bean
    public Docket docket(Environment environment) {
        // 检查springboot当前环境, 根据环境的不同,是否允许访问swagger-ui.html
        Profiles pf = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(pf);

        // 修改Swagger页面默认配置
        return new Docket(DocumentationType.SWAGGER_2)
                // 修改默认字段
                .apiInfo(apiInfo())
                // 是否允许访问swagger-ui.html页面(一般生产环境不允许打开该页面)
                .enable(flag)
                // 不同组, 后端多人开发, 每人使用不同的组, 方便前端测试时进行区分
                .groupName("huzhongbin")
                // 设置只对某个包使用Swagger
                .select().apis(RequestHandlerSelectors.basePackage("com.ll.swaggerdemo")).build();
    }

    // Docket对象1
    @Bean
    public Docket docket1(Environment environment) {
        return new Docket(DocumentationType.SWAGGER_2).groupName("huzhongbin-docket1");
    }
    // 修改swagger-ui.html页面默认信息
    private ApiInfo apiInfo() {
        return new ApiInfo("Api Documentation"
                , "Api Documentation -- 123"
                , "1.0"
                , "urn:tos"
                , new Contact("1", "2", "3")
                , "Apache 2.0"
                , "http://www.apache.org/licenses/LICENSE-2.0"
                , new ArrayList());
    }
}
View Code

 

Swagger简单使用

 

 

Swagger简单使用

 

上一篇:Swagger


下一篇:Swagger的使用