SSM整合swagger2

    xiaoxiao2022-07-13  174

    1.Swagger2介绍

    1.1、概念

    Swagger2可以减少我们创建文档的工作量,同时说明内容又整合入实现代码中,让维护文档和修改代码整合为一体, 可以让我们在修改代码逻辑的同时方便的修改文档说明。

    另外Swagger2也提供了强大的页面测试功能来调试。

    解决的问题:

    由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型、HTTP头部信息、HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下游的抱怨声不绝于耳。

    随着时间推移,不断修改接口实现的时候都必须同步修改接口文档,而文档与代码又处于两个不同的媒介,除非有严格的管理机制,不然很容易导致不一致现象。

    2.SSM框架下的配置

        Maven依赖配置 在 pom.xml 中添加依赖

    <!--swagger是一款方便展示的API文档框架,2.8.0为2018年版本--> <dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger2</artifactId>    <version>2.8.0</version> </dependency> <dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger-ui</artifactId>    <version>2.8.0</version> </dependency> <!--springfox依赖的jar包;如果你的项目中已经集成了无需重复--> <dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>2.9.0</version> </dependency>

     

    2.在源码目录下创建一个单独的package,然后创建SwaggerConfig.java文件

     

    @Configuration @EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = {"com.xxx.controller.*"})//扫描control所在的package请修改为你control所在package public class SwaggerConfig extends WebMvcConfigurationSupport {     @Bean     public Docket createRestApi() {         String environment = ConfigProperty.getInstance().getENVIRONMENT();         Docket docket = new Docket(DocumentationType.SWAGGER_2);         ApiInfo apiInfo = new ApiInfoBuilder()                 .title("[主站项目接口文档]")                 .description("测试环境接口测试")                 .version("1.0")                 .build();                 docket.apiInfo(apiInfo);                 //设置只生成被Api这个注解注解过的Ctrl类中有ApiOperation注解的api接口的文档         if(environment.equals("master")) {             //生产环境下接口文档关闭不展示             docket.select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))                     .paths(PathSelectors.none())                     .build();         }else{             //测试环境接口展示             docket.select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))                     .paths(PathSelectors.any())                     .build();         }         return docket;     } }

     

    3、在springMVC的配置文件中配置swagger

    <!-- 自动扫描swagger配置文件,写入swagger资源路径映射 --> <bean id="swaggerconfig" class="com.swagger.SwaggerConfig"></bean> <!--重要!配置swagger资源不被拦截--> <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

    4. 在接口上添加注解

    @Controller @RequestMapping(value = "authorBindPlatform") @Api(tags = "AuthorBindPlatform",description = "作者绑定平台") public class AuthorBindPlatformController extends BaseAct { @RequestMapping(value = "platformInfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8") @ResponseBody @ApiOperation(value = "作者绑定平台信息") @ApiImplicitParam(paramType="query",name="siteType",value ="平台类型,siteType 为空时,默认所有平台",required=false,dataType = "int") @ApiResponses(value = {@ApiResponse(code = 500,message = ResultCode.SUCCESS_MESSAGE,response = String.class)}) public String platformInfo(int siteType) {     // 获取作者信息     try {

    ............

    }

    }

    5、controller的配置,这里我只做简单的配置测试swagger是否正常工作

    开启项目浏览器输入http://localhost:8081/swagger-ui.html

    最新回复(0)