SpringMVC整合Swagger(非Spring Boot)

1.新建项目

1.1 maven项目

image

1.2 设置项目名

image

1.3 设置本地maven

image

1.4 finish

2.添加依赖

打开pom文件,添加下面的依赖

[sourcecode language='xml'  padlinenumbers='true']
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
[/sourcecode]

3.添加Swagger的配置类

[sourcecode language='java' ]
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.whu505.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Person RESTful API文档")
                .description("Person Controller的文档")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

}
[/sourcecode]

猜你喜欢

转载自www.cnblogs.com/GIS-Lu/p/9774534.html