此前写过一个关于SpringBoot集成Swagger的帖子,因为有的项目是SpringMVC的,所以也简单整理了一下,基本一致。
本例使用的是spring 4.1.6版本
1、添加POM依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<!-- 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类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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、配置文件添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<? 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方法
1
2
3
4
5
6
7
8
9
10
11
|
@Controller public class HomeController { @RequestMapping (value = "/test" , method = RequestMethod.GET) @ResponseBody public String test(Locale locale, Model model) { return "test" ; } } |
5、启动服务访问查看效果
访问地址:http://localhost:8188/{工程contextPath}/swagger-ui.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/catoop/article/details/58630864