其他分享
首页 > 其他分享> > Swagger整合到项目

Swagger整合到项目

作者:互联网

1、依赖

 

父模块进行版本控制

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zjazn</groupId>
    <artifactId>gopoint</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.3.RELEASE</version>
    </parent>
    <modules>
        <module>point-service</module>
        <module>point-common</module>
    </modules>
    <packaging>pom</packaging>



    <!--统一管理jar包版本-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

   <dependencyManagement>
       <!--子模块继承之后,提供作用:锁定版本+子module不用groupId和version-->
       <dependencies>
           ....


           <!--swagger-->
           <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger2</artifactId>
               <version>2.7.0</version>
           </dependency>
           <!--swagger ui-->
           <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger-ui</artifactId>
               <version>2.7.0</version>
           </dependency>
       </dependencies>
   </dependencyManagement>




</project>

可以直接写入以下依赖或者间接依赖(写在common,然后项目再依赖common)

<!--Swagger-UI API文档生产工具-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>

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

 

2、写配置类

SwaggerConfig.java  :你可以将这个配置类放在common中
package com.zjazn.gopoint.common;

import com.google.common.base.Predicates;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2 //swagger注解
@RestController
public class SwaggerConfig {
    @Resource
    private RestTemplate restTemplate;
    //添加一个组
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                //配置组名,是否雇用这个组, 组的信息配置ApiInfo,其它配置
                .groupName("groupA").enable(true).apiInfo(webApiInfo()).select()
                //指定扫描的路径:any()扫描全部   none() 不扫描  basePackage() 指定要扫描的包
                .apis(RequestHandlerSelectors.any())
                //过滤哪个路径:paths() 过滤什么路径
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("小社区API文档")
                .description("编码人:小庄,本文档描述了微服务接口定义")
                .version("1.0")
                .contact(new Contact("小庄的BLog", "https://www.cnblogs.com/zjazn/", "2119299531@qq.com"))
                .build();
    }

    /*
    * Swagger注释的使用:
    *       Model:  类:@ApiModel("用户实体类")   属性: @ApiModelProperty("用户名")    ,但Controller 返回的这个Model时,会在SwaggerUI的Model上显示。
    *
    *       Controller:  接口方法上:  @ApiOperation("根据ID获取用户信息")
    *
    *
    * */





}

3、在启动类加入

@EnableSwagger2 注解


然后访问:http://127.0.0.1:ip/swagger-ui.html

4、解决  SpringBoot使用Swagger2出现Unable to infer base url.

 

可能由以下几个原因造成:
  1. 需要在SpringBoot的启动Application前面加上 @EnableSwagger2注解;
  2. 可能是由于使用了Spring Security 影响:尝试使用以下Spring Security配置解决:
import  org.springframework.context.annotation.Configuration;
import  org.springframework.security.config.annotation.web.builders.HttpSecurity;
import  org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
class  SecurityConfig  extends  WebSecurityConfigurerAdapter  {
    private  static  final  String[]  AUTH_WHITELIST  =  {
            // -- swagger ui
            "/swagger-resources/**",
            "/swagger-ui.html",
            "/v2/api-docs",
            "/webjars/**"
    };
    @Override
    protected  void  configure(HttpSecurity http)  throws  Exception  {
        http.authorizeRequests()
                .antMatchers(AUTH_WHITELIST).permitAll()
                .antMatchers("/**/*").denyAll();
    }
}

 

标签:Swagger,项目,springframework,swagger,整合,org,import,springfox,annotation
来源: https://www.cnblogs.com/zhuangjie/p/16373779.html