RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
怎么配置swagger

本篇内容主要讲解“怎么配置swagger”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么配置swagger”吧!

创新互联建站专业为企业提供东河网站建设、东河做网站、东河网站设计、东河网站制作等企业网站建设、网页设计与制作、东河企业网站模板建站服务,十余年东河做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

maven依赖:


    io.springfox
    springfox-swagger2
    2.8.0


    io.springfox
    springfox-swagger-ui
    2.8.0

swagger配置类

import org.springframework.beans.factory.annotation.Value;
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;

/**
 * 配置Swagger,用于测试rest,默认只在本地环境开启
 *
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {

    /**
     * 是否开启swagger
     */
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    /**
     * 扫描对应的包路径,生成API
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled).select()
                // 要扫描的包
                .apis(RequestHandlerSelectors.basePackage("cn.com.test.channel.controller"))
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

    /**
     * 设置API 信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx")
                .description("xxx")
                .version("1.0.0")
                .build();
    }
}

常用注解:

  • Api

  • ApiModel

  • ApiModelProperty

  • ApiOperation

  • ApiParam

  • ApiResponse

  • ApiResponses

  • ResponseHeader

Ex:

@Data
@ApiModel
public class UserRepay {

    @NotNull(message = "用户ID不能为空")
    @ApiModelProperty("用户ID")
    private Long userId;
}

@RestController
@Api(tags = "还款管理", description = "还款管理API")
@RequestMapping("repay")
public class UserRepayController extends BaseController {

    @Resource
    private UserRepayService userRepayService;

    @ApiOperation(value = "获取还款列表", notes = "获取还款列表 userId用户ID必填")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "pageNumber", value = "页码", dataType = "Long", paramType = "query")
    })
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Object get(Long pageNumber, Long pageSize,
                      @RequestBody @ApiParam(value = "还款") UserRepay userRepay) {
        return ResponseUtil.getSuccessMap(userRepayService.get(pageNumber, pageSize, userRepay));
    }
}

到此,相信大家对“怎么配置swagger”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


网页标题:怎么配置swagger
URL标题:http://sczitong.cn/article/jpecce.html