springboot+springfox+Swagger 实现项目的restful文档的自动生成

本文不做赘述,只是简单的让读者知道如何快速的使用

第一步
在springboot项目的启动根目录也就是springboot的启动类同包下创建类ApplicationSwaggerConfig代码如下

import org.springframework.context.annotation.Configuration;

import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {

}

然后在同包下创建SpringConfigAdapter代码如下


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@ComponentScan
@Import(ApplicationSwaggerConfig.class)
public class SpringConfigAdapter extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

代码添加完毕,启动当前项目

访问http://localhost:8080/swagger-ui.html
即可看到页面了
页面截图如下
springboot+springfox+Swagger 实现项目的restful文档的自动生成_第1张图片

至此项目文档的自动生成已经完成

附加该项目运行时需要的所有jar依赖


        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-coreartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-spiartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-commonartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.plugingroupId>
            <artifactId>spring-plugin-coreartifactId>
            <version>1.2.0.RELEASEversion>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-spring-webartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-schemaartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.swaggergroupId>
            <artifactId>swagger-modelsartifactId>
            <version>1.5.12version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.plugingroupId>
            <artifactId>spring-plugin-metadataartifactId>
            <version>1.2.0.RELEASEversion>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.github.robwingroupId>
            <artifactId>swagger2markupartifactId>
            <version>0.9.2version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>io.swaggergroupId>
            <artifactId>swagger-parserartifactId>
            <version>1.0.25version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.swaggergroupId>
            <artifactId>swagger-annotationsartifactId>
            <version>1.5.12version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.swaggergroupId>
            <artifactId>swagger-compat-spec-parserartifactId>
            <version>1.0.25version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>

你可能感兴趣的:(java,spring,Swagger)