其他分享
首页 > 其他分享> > Swagger2的使用

Swagger2的使用

作者:互联网

1.首先需要在对应的服务中进行导包:

<!--引入swagger支持-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.添加成为spring的容易信息,启动的使用,并且修改要生成的服务路径:

package cn.itsource.hrm.config;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2  // 开启Swagger
public class Swagger2 {
    //把对象交给spring管理
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //对外暴露服务的包,以controller的方式暴露,所以就是controller的包. 指定是哪些POM对应包的路径
                .apis(RequestHandlerSelectors.basePackage("cn.itsource.hrm.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("系统服务api")
                .description("系统服务接口文档说明")
                .contact(new Contact("luy668", "http:www.baidu.com", "mlg@itsource.cn"))
                .version("1.0")
                .build();
    }

}

3.根据服务的端口访问swagger即可:

http://localhost:2010/swagger-ui.html#/department-controllericon-default.png?t=M1H3http://localhost:2010/swagger-ui.html#/department-controller

 

 

标签:swagger,documentation,Swagger2,controller,使用,itsource,import,springfox
来源: https://blog.csdn.net/weixin_43938712/article/details/123027345