Maven整合swagger2步骤及注解使用

一. 首先在pom文件中导入swagger依赖

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

二. 创建swagger2的配置类

@Configuration
@EnableSwagger2
@EnableWebMvc
public class Swagger2Config {
    
    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     * 
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.fzss"))  //要扫描的接口的包路径
                .paths(PathSelectors.any())
                .build();
    }
    
    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("欢迎使用Swagger2!")
                .description("~图形化操作界面~")
                .version("1.0")
                .build();
    }
}

三–创建WebConfig类

@Configuration
@EnableWebMvc
@ComponentScan("com.fzss")
public class WebConfig 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/");
    }
}

四–在spring配置类中注入swagger配置类

<!-- swagger配置信息 -->
<bean class="swagger配置类路径" />

五–配置完成后重启服务,输入地址

springboot工程格式如下:
	http://${ip}:{port}/swagger-ui.html

非springboot工程加个自己项目名
	http://${ip}:{port}/xxx/swagger-ui.html

六.需要注意的问题

web.xml 中mvc的urlParten
/* 代表匹配所有的请求
.do 代表匹配所有.do结尾的请求

可以添加一条映射信息,用来使用swagger(如果是 /* 就不用添加)

  <servlet-mapping>
      <servlet-name>SpringMVC</servlet-name>
      <url-pattern>*.html</url-pattern>
  </servlet-mapping>

·····················································································································································
·····················································································································································
·····················································································································································

Swagger2的注解使用:

@Api:用在类上,说明该类的作用。

@ApiOperation:注解来给API增加方法说明。

@ApiImplicitParams:用在方法上包含一组参数说明。

@ApiImplicitParam:用来注解来给方法入参增加说明。

@ApiResponses: 用于表示一组响应。

@ApiResponse: 用在@ApiResponses中,一般用于表达一个错误的响应信息。

code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类

@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值