Swagger2+SpringMVC整合

swagger是什么以及使用的好处,网上随便搜索就有,就不赘述了,下面主要说明swagger和springmvc配置的步骤。

首先声明一点,由于配置过程中查看了较多的文章,无法一一标明引用地址,在此感谢各位的分享

经过一天多的搜索,swagger现在在网上有大致两种,一种是springfox-swagger
Swagger2+SpringMVC整合_第1张图片
另一种就是springfox-swagger2
Swagger2+SpringMVC整合_第2张图片
两种教程都很多,配置也都大概一致,但是会有一些细微的差别,在此做个全面的总结。
环境及工具:JDK 1.8 Intellij IDEA 2017.2 maven 3.3.9 tomcat 8

maven依赖:

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.5.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.5.0version>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>2.9.5version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.5version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.9.5version>
        dependency>
        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>15.0version>
        dependency>
        <dependency>
            <groupId>com.fasterxmlgroupId>
            <artifactId>classmateartifactId>
            <version>1.1.0version>
        dependency>
         

swagger配置

package com.qinzx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;

/**
 * @author: qinzx
 * @date: 2018/5/30 14:03
 * @modify by:
 */
@Component
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.qinzx.controller")
public class Swagger2Config {
    @Bean
    public Docket createAPI() {
        return new Docket(DocumentationType.SWAGGER_2).forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
                //过滤生成链接
                .paths(PathSelectors.any()).build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {

        Contact contact=new Contact("赵帅","http://blog.maileba.top","[email protected]");
        ApiInfo apiInfo = new ApiInfoBuilder().license("Apache License Version 2.0").title("Swagger 集成测试").description("Swagger API Teste").contact(contact).version("1.0").build();

        return apiInfo;
    }

}

sping-context.xml配置

    
    <context:component-scan base-package="com.qinzx.controller"/>
    <context:component-scan base-package="springfox"/>
    
    <mvc:default-servlet-handler />

    
    <mvc:annotation-driven />
    <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
    <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>

controller注解

@ApiOperation(value = "根据用户名获取用户对象", httpMethod = "GET", response = ResponseMsg.class, notes = "根据用户名获取用户对象")
    @RequestMapping("/list")
    @ResponseBody
    public ResponseMsg getList() {
        return new ResponseMsg().success(new ArrayList());
    }

配置好这些,访问地址各式为{ip}:{port}/{project-name}/swagger-ui.html,没有project-name就省略,例如:

http://localhost:8088/swagger-ui.html

暂时就这些,有疑问欢迎留言。

天下武功,唯快不破!

你可能感兴趣的:(插件)