SpringMVC集成Swagger實(shí)例代碼
此前寫過(guò)一個(gè)關(guān)于SpringBoot集成Swagger的帖子,因?yàn)橛械捻?xiàng)目是SpringMVC的,所以也簡(jiǎn)單整理了一下,基本一致。
本例使用的是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這個(gè)是一個(gè)配置文件類,這里配置的Bean要交給Spring去管理。這個(gè)就是用來(lái)取代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())//過(guò)濾的接口 .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("接口服務(wù)")// 大標(biāo)題 .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"> <!-- 這里省略了其他原來(lái)的配置內(nèi)容 --> ...... ...... ...... ...... <mvc:default-servlet-handler /> </beans:beans>
4、測(cè)試Controller方法
@Controller public class HomeController { @RequestMapping(value = "/test", method = RequestMethod.GET) @ResponseBody public String test(Locale locale, Model model) { return "test"; } }
5、啟動(dòng)服務(wù)訪問(wèn)查看效果
訪問(wèn)地址:http://localhost:8188/{工程contextPath}/swagger-ui.html
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springMVC利用FastJson接口返回json數(shù)據(jù)相關(guān)配置詳解
- springmvc fastjson 反序列化時(shí)間格式化方法(推薦)
- 詳解在springmvc中解決FastJson循環(huán)引用的問(wèn)題
- Spring MVC集成springfox-swagger2構(gòu)建restful API的方法詳解
- Spring MVC利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API詳解
- SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法
- SpringMVC和Swagger整合方法
- SpringMVC 中配置 Swagger 插件的教程(分享)
- Spring MVC+FastJson+Swagger集成的完整實(shí)例教程
相關(guān)文章
java中實(shí)現(xiàn)控制臺(tái)打印sql語(yǔ)句方式
這篇文章主要介紹了java中實(shí)現(xiàn)控制臺(tái)打印sql語(yǔ)句方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06SpringBoot 入門教程之引入數(shù)據(jù)傳輸層的方法
這篇文章主要介紹了SpringBoot 入門教程之引入數(shù)據(jù)傳輸層的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Mybatis注解方式完成輸入?yún)?shù)為list的SQL語(yǔ)句拼接方式
這篇文章主要介紹了Mybatis注解方式完成輸入?yún)?shù)為list的SQL語(yǔ)句拼接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11springboot開(kāi)發(fā)flowable定時(shí)任務(wù)問(wèn)題
這篇文章主要介紹了springboot開(kāi)發(fā)flowable定時(shí)任務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦)
這篇文章主要介紹了Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Spring MVC url提交參數(shù)和獲取參數(shù)
本文重要講述通過(guò)url提交參數(shù)和獲取參數(shù)的具體操作與實(shí)現(xiàn)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04Java如何基于okhttp請(qǐng)求SSE接口流式返回詳解
對(duì)于流式返回,Spring Boot提供了兩種不同的方式,下面這篇文章主要給大家介紹了關(guān)于Java如何基于okhttp請(qǐng)求SSE接口流式返回的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03