其他分享
首页 > 其他分享> > 005.SpringBoot-RESTful-API-接口文档组件Swagger2

005.SpringBoot-RESTful-API-接口文档组件Swagger2

作者:互联网

添加依赖

<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>

启动类添加注解@EnableWebMvc 即可

package com.java.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@SpringBootApplication
public class CoreApplication {
    //项目启动时执行的方法
    public static void main(String[] args) {
        //SpringApplication.run(CoreApplication.class, args);
        // 获取 Spring Boot 上下文
        ConfigurableApplicationContext ctx = SpringApplication.run(CoreApplication.class, args);
        //获取配置的端口
        //System.out.println(ctx.getEnvironment().getProperty("server.port"));
        //加密数据自动解密
        System.out.println(ctx.getEnvironment().getProperty("admin.password"));

        //配置类的读取
        //Config cg = (Config) ctx.getBean("config");
        //ctx.close();
    }

}

新建Swagger配置文件,需要和启动类在一个级别

 


@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.java.core"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("SpringBoot整合Swagger")
                        .description("SpringBoot整合Swagger,详细信息......")
                        .version("9.0")
                        .contact(new Contact("啊啊啊啊","blog.csdn.net","aaa@gmail.com"))
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build());
    }
}

新建SwaggerUI自动发布类,建议和上面的类也是在同一级

package com.java.core;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * 访问静态资源
     * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * SpringBoot自动配置本身并不会把/swagger-ui.html
         * 这个路径映射到对应的目录META-INF/resources/下面
         * 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布;
         */
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
        super.addResourceHandlers(registry);
    }
}

访问接口文档

http://localhost:8080/swagger-ui.html

 这里可以直接调试接口

 配置介绍

接口配置

@Api(tags = "用户管理")
@ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "用户编号", required = true, example = "1")
@ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")

 

 排序

 

代码分组

 

详细分组

 

 

 

 

标签:SpringBoot,springframework,API,static,Swagger2,org,import,com,public
来源: https://blog.csdn.net/qq873113580/article/details/122385996