zoukankan      html  css  js  c++  java
  • springmvc 与 springfox-swagger2整合

    一、pom.xml引入基于maven的swagger依赖

         <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.7</version>
            </dependency>

    二、编写SwaggerConfig配置类

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    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;
     
    @Configuration
    @EnableSwagger2
    @EnableWebMvc
    @ComponentScan(basePackages = {"com.foriseland.fsoa.pay.controller"})
    public class SwaggerConfig {
         @Bean
            public Docket customDocket() {
                return new Docket(DocumentationType.SWAGGER_2)
                        .apiInfo(apiInfo())      
                        .select()
                        .apis(RequestHandlerSelectors.any())
                        .paths(PathSelectors.any())
                        .build();
            }
     
            private ApiInfo apiInfo() {
                Contact contact = new Contact("支付组", "", "");
                return new ApiInfoBuilder()
                        .title("支付API接口")
                        .description("支付API接口")
                        .contact(contact)
                        .version("1.1.0")
                        .build();
            }
    }

    三、在spring mvc配置xml里面加入Swagger配置

      <!-- swagger -->
       <bean class="com.foriseland.fsoa.pay.swagger.SwaggerConfig"></bean>
       <mvc:default-servlet-handler/>
       <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
       <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

    四、controller层的编写例子

    @Controller
    @CrossOrigin
    @Api(tags="账户信息控制类")
    @RequestMapping("account")
    public class AccountController{
    
        @ApiOperation(value = "获取盈豆数量", notes = "获取盈豆数量", httpMethod = "POST")
        @RequestMapping(value="getBeansNumber", method=RequestMethod.POST)
        @ResponseBody
        public void getBeansNumber(@RequestBody BeansCountDto info, HttpServletResponse response) {
     
        }
    }

    五、启动项目

    http://127.0.0.1:8080/{projectName}/swagger-ui.html

    参考:https://blog.csdn.net/lovelichao12/article/details/79387569

  • 相关阅读:
    一个关于STL list使用 小示例
    c++几个通用工具
    修改pc机的mac地址 以及 mac地址的组成
    win7 下 arp 绑定mac和Ip
    wireshark如何抓取别人电脑的数据包
    WPA破解原理简要——无线网络破解续
    Apache配置代理服务器的方法(2)
    Apache配置代理服务器的方法(1)
    在Linux系统中如何设置APACHE服务器里的后台页面只允许某个IP地址访问
    Apache服务器配置技巧
  • 原文地址:https://www.cnblogs.com/vvonline/p/10020596.html
Copyright © 2011-2022 走看看