swagger框架运用

Swagger功能强大,UI界面漂亮,并且支持在线测试,在项目中运用swagger方便项目测试,以下是springmvc中配置swagger框架。
一、引入Swagger相关的jar包

<!-- springMVC框架 start -->
.......
<!-- springMVC框架 start -->


<!-- swagger框架 start -->
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-springmvc</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-models</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
    <groupId>com.wordnik</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>1.3.11</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>15.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml</groupId>
    <artifactId>classmate</artifactId>
    <version>1.1.0</version>
</dependency>
<!-- swagger框架 end -->

二、在springmvc配置文件(spring-mvc.xml)中加入swagger相关的配置

<!-- Swagger 配置 -->
<bean  class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
<mvc:resources mapping="/swagger/**" location="/WEB-INF/swagger/"/> 

三、在项目中加入swagger配置类

package com.eakom.swagger;
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Swagger 配置类
 * @author eakom
 * @date 2017年12月1日
 */
@Configuration
@EnableSwagger
public class SwaggerConfig {


    private SpringSwaggerConfig springSwaggerConfig;


    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
    {
        this.springSwaggerConfig = springSwaggerConfig;
    }


    /**
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
     * framework - allowing for multiple swagger groups i.e. same code base
     * multiple swagger resource listings.
     */
    @Bean
    public SwaggerSpringMvcPlugin customImplementation()
    {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
                .apiInfo(apiInfo())
                .includePatterns(".*?");
    }


    private ApiInfo apiInfo()
    {
        ApiInfo apiInfo = new ApiInfo(
                "My Apps API Title",
                "My Apps API Description",
                "My Apps API terms of service",
                "My Apps API Contact Email",
                "My Apps API Licence Type",
                "My Apps API License URL");
        return apiInfo;
    }
}

四、在webapp/WEB-INF下加入swagger插件代码,swagger代码下载链接如下
http://download.csdn.net/download/eakom/10140984
五、找到webapp/WEB-INF/swagger文件中的index.html文件,打开把第40行代码改为
url = “http://localhost:8080/[ProjectName]/api-docs“;注意:[ProjectName]为项目名,re:我的项目名为OA,则URL为http://localhost:8080/OA/api-docs
这里写图片描述

六、在Controller中加入相关注解
1.类名前加入@Api(value=”/security”,description=”安全校验”)
2.方法名前加入@ApiOperation(value = “用户登陆校验”, notes = “校验用户登录信息,默认返回该用户信息”)
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值