SpringBoot配置自定義美化Swagger2


1.添加maven依賴

 

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
 
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
 
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.8.9</version>
        </dependency>

2.添加swagger2配置文件

@Configuration
public class Swagger2 {
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xiaokang.login_MP.controllor"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger構建api文檔")
                .description("簡單優雅的restfun風格,")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}

 

3.對接口增加描述

@ApiOperation(value = "登錄頁面", notes = "index登錄頁面")
    @RequestMapping("index")
    public String index() {
        return "index";
    }

更多使用看最后資料

 

4.開啟Swagger2注解

@MapperScan(value = "com.xiaokang.login_MP.dao")
@EnableSwagger2
@EnableSwaggerBootstrapUI
@SpringBootApplication
public class LoginMpApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(LoginMpApplication.class, args);
    }
 
}

 

5.登錄 http://localhost:8080/doc.html 來查看界面

 

 

 

 

6.如果 輸入 http://localhost:8080/doc.html 后顯示404,找不到頁面則需要配置一下

@Configuration
public class WebConf extends WebMvcConfigurationSupport {
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 將所有/static/** 訪問都映射到classpath:/static/ 目錄下
        registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/").addResourceLocations("classpath:/templates/")
                .addResourceLocations("classpath:/public/");
        // swagger2
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/docs.html").addResourceLocations("classpath:/META-INF/resources/");
 
        super.addResourceHandlers(registry);
    }
}

 

7.Swagger2注解

swagger通過注解表明該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。

  • @Api:修飾整個類,描述Controller的作用
  • @ApiOperation:描述一個類的一個方法,或者說一個接口
  • @ApiParam:單個參數描述
  • @ApiModel:用對象來接收參數
  • @ApiProperty:用對象接收參數時,描述對象的一個字段
  • @ApiResponse:HTTP響應其中1個描述
  • @ApiResponses:HTTP響應整體描述
  • @ApiIgnore:使用該注解忽略這個API
  • @ApiError :發生錯誤返回的信息
  • @ApiImplicitParam:一個請求參數
  • @ApiImplicitParams:多個請求參數

 

8.更多參考鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM