资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Spring4.2.2以上版本和swagger集成方案和踩过的坑-创新互联

因为公司使用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比较低的,为了使朋友们少踩坑,我这边将集成的过程记录一下:

目前创新互联公司已为上千的企业提供了网站建设、域名、虚拟空间、网站托管、服务器租用、企业网站设计、城关网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

1. 引入spring、swagger的相关jar包(springfox-swagger2、springfox-swagger-ui),在pom.xml中配置:

  
            io.springfox  
            springfox-swagger2  
            2.4.0  
              
                  
                    org.springframework  
                    spring-core  
                  
                  
                    org.springframework  
                    spring-beans  
                  
                  
                    org.springframework  
                    spring-context  
                  
                  
                    org.springframework  
                    spring-context-support  
                  
                  
                    org.springframework  
                    spring-aop  
                  
                  
                    org.springframework  
                    spring-tx  
                  
                  
                    org.springframework  
                    spring-orm  
                  
                  
                    org.springframework  
                    spring-jdbc  
                  
                  
                    org.springframework  
                    spring-web  
                  
                  
                    org.springframework  
                    spring-webmvc  
                  
                  
                    org.springframework  
                    spring-oxm  
                  
              
          
          
            io.springfox  
            springfox-swagger-ui  
            2.4.0  
        

提醒: 特别注意,springfox-swagger2在集成的时候,已经引入了spring的相关jar,特别是spring-context、spring-context-support的版本和项目中使用的版本完全不一致,项目在启动的时候出现很多包冲突的问题,这边在引入pom.xml文件的时候过滤掉了spring的相关jar包。

2. 编写Swagger的配置类:

package com.ml.honghu.swagger.web;  
  
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;  
  
@EnableWebMvc  
@EnableSwagger2  
@Configuration  
@ComponentScan(basePackages ={"com.ml.honghu.**.rest"})  
public class SwaggerConfig {  
    @Bean  
    public Docket createRestApi() {  
        return new Docket(DocumentationType.SWAGGER_2)  
                .apiInfo(apiInfo())  
                .select()  
                .apis(RequestHandlerSelectors.basePackage("com.ml.honghu"))  
                .paths(PathSelectors.any())  
                .build();  
    }  
  
    private ApiInfo apiInfo() {  
        return new ApiInfoBuilder()  
                .title("接口列表 v1.0")  
                .description("接口信息")  
                .termsOfServiceUrl("http://honghu.com")  
                .contact(new Contact("", "", "HongHu"))  
                .version("1.1.0")  
                .build();  
    }  
}

3. 在spring-mvc.xml文件中进行过滤器的配置,过滤掉swagger的相关访问配置:

  
  

4. 服务配置项

@Api("区域服务")
@RestController  
@RequestMapping(value = "/rest/area")  
public class AreaService  {  
  
    @Autowired  
    private AreaService areaService;  
      
    @ApiOperation(value = "区域列表", httpMethod = "GET", notes = "区域列表")  
    @IsLogin  
    @ResponseBody  
    @RequestMapping(value = "treeData", method = RequestMethod.GET)  
    public List> treeData(  
            @ApiParam(required = true, value = "区域ID")  @RequestParam(required=false) String extId, HttpServletResponse response) {  
        List> mapList = Lists.newArrayList();  
        List list = areaService.findAll();  
        for (int i=0; i map = Maps.newHashMap();  
                map.put("id", e.getId());  
                map.put("pId", e.getParentId());  
                map.put("name", e.getName());  
                mapList.add(map);  
            }  
        }  
        return mapList;  
    }  
}

启动项目,查看结果:

Spring 4.2.2以上版本和swagger集成方案和踩过的坑 资料和源码来源  


网站题目:Spring4.2.2以上版本和swagger集成方案和踩过的坑-创新互联
网页地址:http://cdkjz.cn/article/ddopsi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220