swagger配置

swagger是用来自动生成api文档的工具,还能兼测试请求的工具
首先在maven中导入以下依赖

<dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>19.0</version>
    </dependency>
    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-jdk8</artifactId>
      <version>1.1.0.Final</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.7</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.7</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.8.7</version>
    </dependency>

创建一个swagger-config用来配置信息

@Configuration// 写了applicationContext-mybatis.xml
@ComponentScan(basePackages="controller") // <context-ComponentScan basepackage="xxx.xx.x.xx">
@EnableSwagger2//启动swaggerapi生成
public class SwaggerConfig extends WebMvcConfigurationSupport{
    @Bean// <bean id="apiGen" class="Docket"/>

    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

    }
    /**
     * 创建该Api的基本信息(这些基本信息会展现在文档页面中)
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("这是标题")
                .termsOfServiceUrl("")
                .contact("这是小组")
                .version("这是版本")
                .build();
    }
}

需要在spring-mvc的配置文件中指定Swagger-config工具文件的路径

 <bean class="util.SwaggerConfig"/>

最后在网页输入地址+后缀(本地测试: http://localhost:8080/swagger-ui.html)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值