配置swagger2.x,测试访问

swagger2.x配置

  • 导入jar包
  • SwaggerConfig配置类
  • 访问swagger

导入jar包

两个swagger的UI加一个就行.

<!--  swagger依赖 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!--  swagger官方提供的UI() -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.9.2</version>
</dependency>
 <!--swagger优化后的UI依赖(访问方式:http://localhost:{IP}/{context-path}/swagger-ui.html)-->
<dependency>
     <groupId>com.github.xiaoymin</groupId>
     <artifactId>swagger-bootstrap-ui</artifactId>
     <version>1.9.6</version>
 </dependency>
 

SwaggerConfig配置类

这里是配置了多个模块,如果你只有一个模块,配置一个Bean就行。
注意SwaggerConfig配置类需要放在spring扫描包能扫描的路径下,如果不在需要在启动类型使用@ComponentScan扫描到,或者在resource.META-INF.spring.factories中配置org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xx.xx.xx.SwaggerConfig,总之能将配置的Bean注入spring就行。

import io.swagger.annotations.Api;
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;

@Configuration
@EnableSwagger2
public class Swaggerconfig {

    @Value("${swagger2.enable}")
    private boolean enable;

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger演示项目")
                .description("swagger接口测试工具")
                .contact("hippoDocker")
                .version("1.0")
                .build();
    }

    @Bean("模块一")
    public Docket controller1Apis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("模块一")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/controller1.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(enable);
    }

    @Bean("模块二")
    public Docket controller2Apis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("模块二")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/controller2.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(enable);
    }

}

因为这里分组配置了中文,需要在配置里面指定编码,没有使用中文就不用管。

server:
  port: 8080
  tomcat:
    uri-encoding: UTF-8
    max-threads: 800
    min-spare-threads: 30
    max-connections: 10000
  servlet:
    context-path: /lear-swagger

spring:
  http:
    encoding:
      charset: UTF-8
      force: true
      enabled: true
      
# swagger开启配置
swagger2:
  enable: true

访问swagger

默认UI访问地址:
3.0.x访问地址:
http://localhost:8080/{context-path}/swagger-ui/index.html
2.9.x访问地址(本次访问地址):
http://localhost:8080/{context-path}/swagger-ui.html

使用swagger-bootstrap-ui的swaggerUI,访问地址:
http://localhost:8080/{context-path}/doc.html

你可能感兴趣的:(java,#,Spring,java,spring,spring,boot)