一步一步教你SSM整合swagger
阿新 • • 發佈:2018-10-24
api文檔 ces desc ref jackson nconf 容器 cat .cn 一:什麽是swagger?
?swagger是一款非常好用的寫API文檔的框架。其他自行百度
二:ssm整合swagger?
1:在maven的pom文件中引入依賴:(註意版本,否則會導致tomcat不能正常啟動)
<!-- 引入swagger --> <!--springfox的核心jar包 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <!--springfox-ui的jar包(裏面包含了swagger的界面靜態文件) --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <!--springfox依賴的jar包;如果你的項目中已經集成了無需重復 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency>
2:創建SwaggerConfig.java文件、即:swagger的配置文件,最好放到單獨的文件夾下
@WebAppConfiguration @EnableSwagger2 @EnableWebMvc @ComponentScan(basePackages="com.zgz.cn.controller") public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("xxx項目接口文檔") .description("xxx項目接口測試") .version("1.0.0") .termsOfServiceUrl("") .license("") .licenseUrl("") .build(); } }
3:在SpringMVC的配置文件中配置:(配置所有控制器所在的包)
<!-- 配置swagger的bean --> <!-- 將靜態資源交由默認的servlet處理 --> <mvc:default-servlet-handler/> <!-- 向容器自動註入配置 --> <context:annotation-config/> <!-- 將SwaggerConfig配置類註入 --> <bean class="com.zgz.cn.swagger.SwaggerConfig"/> <!-- 配置swagger資源不被攔截 --> <!-- <bean id="swagger2Config" class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"/> --> <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/> <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
4:在web.xml中配置所有的請求都經過DispatcherServlet處理
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5:在控制器中使用註解:(swagger常用註解)
6:測試(訪問地址:項目名/swagger-ui.html )
一步一步教你SSM整合swagger