一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - SpringMVC集成Swagger實例代碼

SpringMVC集成Swagger實例代碼

2020-09-19 16:39catoop JAVA教程

本篇文章主要介紹了SpringMVC集成Swagger實例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

此前寫過一個關于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

SpringMVC集成Swagger實例代碼

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/catoop/article/details/58630864

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产3级在线 | 国产精品久久香蕉免费播放 | 1769国产精品免费视频 | 久久精品手机观看 | 久久青草免费91线频观看站街 | 91青青视频 | 色婷婷综合缴情综六月 | 午夜小视频网站 | 青青青手机视频在线观看 | 免费一级片在线 | 91入口免费网站大全 | 紧身裙女教师波多野结衣 | 性关系免费视频 | 桥本有菜作品在线 | katsumi精品hd | 呜呜别塞了啊抽插 | 91av俱乐部 | 99久久免费国产精品热 | 亚洲 日韩 国产 中文视频 | 欧美一级欧美三级 | 91九色视频无限观看免费 | 亚洲免费视频一区二区三区 | 免费精品99久久国产综合精品 | 国产日韩精品欧美一区 | 久久人妻少妇嫩草AV无码 | 免费观看在线永久免费xx视频 | 男人狂躁女人下面狂叫图片 | 国产精品久久国产三级国电话系列 | 9久热久爱免费精品视频在线观看 | 青青自拍视频 | 男女羞羞的视频 | 四虎在线免费 | 午夜国产在线 | 亚洲高清一区二区三区四区 | 亚洲v日韩v欧美在线观看 | juy799大岛优香在线观看 | 午夜无码片在线观看影院 | 亚洲精品卡一卡2卡3卡4卡 | 天美蜜桃精东乌鸦传媒 | 九9热这里只有真品 | 男女姓交大视频免费观看 |