eclipse 搭建 swagger-ui(maven项目 springboot框架)

1 pom.xml 引入两个jar:

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

2 SpringBootApp.java  添加注解 @EnableSwagger2

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@SpringBootApplication
@EnableSwagger2
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

3 启动项目 访问 http://localhost:8080/swagger-ui.html

最基本的swagger就出来了

4 swaggerConfig.java 配置文件(这样页面中出现的东西我们就可以更改了)

在SpringBoot的启动ApplicationApp.java同目录下创建java文件 swaggerConfig.java

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 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("controller")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.pusamm"))//项目包名
                .paths(PathSelectors.any()).build();
    }
 
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("移动C2M RESTful API")
                .description("swagger-ui")
                .termsOfServiceUrl("").version("1.0").build();
    }
}

 

 页面就变成这样了,其他的根据自己项目需求修改即可。

 

posted @ 2020-09-27 11:13  少司命  阅读(586)  评论(0编辑  收藏  举报