springMVC整合swagger

导包
pom.xml

        <!-- swagger -->
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.1</version>
        </dependency>

配置注解

    <context:component-scan base-package="com.ssh.swagger" />
    <!-- 将 springSwaggerConfig加载到spring容器 -->
    <bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
    <!-- 将自定义的swagger配置类加载到spring容器 -->
    <bean class="com.ssh.swagger.SwaggerConfig" />

配置类

package com.ssh.swagger;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.context.annotation.Bean;  

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;  

@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(  
                "springmvc搭建swagger",  
                "spring-API swagger测试",  
                "My Apps API terms of service",  
                "1043023220@qq.com",  
                "web app",  
                "My Apps API License URL");  
        return apiInfo;  
    }  
}  

Controller.java

@Api(value="user")  
@Controller  
@RequestMapping(value="/person")  
public class PersonController{  

    @Autowired
    private PersonService personService;


    @ApiOperation(value = "根据ID获取用户信息",httpMethod="GET",notes="get user by id",response=Person.class)
    @RequestMapping(value = "/show")
     @ResponseBody
    public Person findById(@ApiParam(required=true,value="用户ID",name="userId")@RequestParam(value="id")Integer id) {
        Person person = personService.getByid(id);
        model.addAttribute("person", person);
        return person;
    }

}  

下载swagger-ui:https://github.com/swagger-api/swagger-ui
解压后将dist文件夹中所有的文件拷贝到webapp/swagger这里的swagger是作者自定义的你可以写为自己创建的目录。

修改index.html中的 http://petstore.swagger.wordnik.com/v2/swagger.json修改为自己项目路径+api-docs,例如:

http://localhost:8080/ssh/api-docs:ssh为项目名称。(如果springMVC配置的拦截路径为*.do则为http://localhost:8080/ssh/api-docs.do )路径要能被springMVC拦截到

在spring-mvc.xml中过滤掉swagger-ui的文件:

<!-- 静态资源文件,不会被Spring MVC拦截 -->  
<mvc:resources mapping="/swagger/**" location="/swagger/" /> 

在浏览器中访问在浏览器中访问:http://localhost:8080/ssh/swagger/index.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值