基于spring-boot使用Swagger构建restful api文档

1.首先是添加maven依赖:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

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;

/**
 * Author: 李阳
 * Date: 2017年12月1日
 * Description:
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kevin.swagger"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger系统RESTful API文档")
                .description("Swagger系统API")
                .termsOfServiceUrl("")
                .contact(new Contact("kevin的文档", null, null))
                .version("0.0.0")
                .build();
    }
}

通过@Configuration注解,让Spring来加载该类配置;
通过@EnableSwagger2注解来启用Swagger2。
通过createRestApi函数创建Docket的Bean;
apiInfo()用来创建该Api的基本信息(展现在文档页面中);
select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现。

3.使用:

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "swagger-demo")
@Api(value = "Swagger信息", description = "Swagger信息服务", hidden = true)
public class SwaggerDemoController {

    @ApiOperation(value = "根据主键获得Swagger信息", httpMethod = "GET", response = ResponseEntityBody.class)
    @ApiImplicitParam(name = "id", value = "Swaggerid", required = true, dataType = "long", paramType = "path")
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    @ResponseBody
    public ResponseEntityBody loadSwaggerInfo(@PathVariable(name = "id", required = true) Long id) {
            ……
    }

    @ApiOperation(value = "查询Swagger信息", httpMethod = "GET", response = ResponseEntityBody.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "SwaggerName", value = "Swagger姓名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "Swaggerer", value = "Swagger号", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET, value = "")
    @ResponseBody
    public ResponseEntityBody loadSwaggers(
            @RequestParam(name = "SwaggerName", required = false) String memberName,
            @RequestParam(name = "Swaggerer", required = false) String cardNumber
    ) {
             ……
    }

    @ApiOperation(value = "添加Swagger", httpMethod = "POST", response = ResponseEntityBody.class)
    @ApiImplicitParam(name = "view", value = "Swagger数据对象", required = true, dataType = "SwaggerView", paramType = "body")
    @RequestMapping(method = RequestMethod.POST, value = "")
    @ResponseBody
    public ResponseEntityBody addSwagger(@RequestBody SwaggerView view) {
          ……
    }
}

paramType:

query: @RequestParam对应的参数;
path:@PathVariable对应的参数,即url中参数;
body:@RequestBody对应的参数。

 

通过http://localhost:8080//swagger-ui.html进行api查看

转载于:https://my.oschina.net/kevin2kelly/blog/1583432

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值