本篇主要介紹了SpringMVC整合Swagger實例程式碼,小編覺得蠻不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧
先前寫過一個關於SpringBoot整合Swagger的帖子,因為有的專案是SpringMVC的,所以也簡單整理了一下,基本上一致。
本範例使用的是spring 4.1.6版本
1、新增POM依賴
<!-- Jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency> <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
2、新增SwaggerConfig.java類別
package com.shanhy.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.google.common.base.Predicate; import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration // 该注解就是告诉Spring这个是一个配置文件类,这里配置的Bean要交给Spring去管理。这个就是用来取代Beans.xml这种文件的。 @EnableSwagger2 // 启用 Swagger public class SwaggerConfig { @Bean public Docket createRestApi() { Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() { @Override public boolean apply(RequestHandler input) { Class<?> declaringClass = input.declaringClass(); // if (declaringClass == BasicErrorController.class)// 排除 // return false; if (declaringClass.isAnnotationPresent(RestController.class)) // 被注解的类 return true; if (input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法 return true; return false; } }; return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) // .genericModelSubstitutes(DeferredResult.class) // .genericModelSubstitutes(ResponseEntity.class) .useDefaultResponseMessages(false) // .forCodeGeneration(false) .select().apis(predicate) // .paths(PathSelectors.any())//过滤的接口 .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("接口服务")// 大标题 .version("1.0")// 版本 .build(); } }
3、設定檔新增
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 这里省略了其他原来的配置内容 --> ...... ...... ...... ...... <mvc:default-servlet-handler /> </beans:beans>
4、測試Controller方法
@Controller public class HomeController { @RequestMapping(value = "/test", method = RequestMethod.GET) @ResponseBody public String test(Locale locale, Model model) { return "test"; } }
5、啟動服務存取檢視效果
存取位址:localhost:8188/{工程contextPath}/swagger-ui .html
【相關推薦】
#1. Java免費影片教學
2. 全面解析Java註解
3. YMP線上手冊
#以上是java整合Swagger的步驟詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!