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

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot集成springfox-swagger2構建restful API的方法教程

Spring Boot集成springfox-swagger2構建restful API的方法教程

2020-11-16 15:24興國First Java教程

這篇文章主要給大家介紹了關于Spring Boot集成springfox-swagger2構建restful API的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。

前言

之前跟大家分享了Spring MVC集成springfox-swagger2構建restful API,簡單寫了如何在springmvc中集成swagger2。這邊記錄下在springboot中如何集成swagger2。其實使用基本相同。

方法如下:

首先還是引用相關jar包。我使用的maven,在pom.xml中引用相關依賴(原來我使用的是2.2.0的,現在使用2.4.0的):

?
1
2
3
4
5
6
7
8
9
10
<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger2</artifactId>
 <version>2.4.0</version>
</dependency>
<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger-ui</artifactId>
 <version>2.4.0</version>
</dependency>

第二步就是創建swagger的配置類:

這個配置類和springmvc的寫法完全一致。為了區分我又重命名一個。

?
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
package com.xingguo.springboot;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
 
 @Bean
 public Docket buildDocket(){
  return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(buildApiInf())
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.xingguo.springboot.controller"))
    .paths(PathSelectors.any())
    .build();
 }
 
 private ApiInfo buildApiInf(){
  return new ApiInfoBuilder()
     .title("xingguo大標題")
     .description("springboot swagger2")
     .termsOfServiceUrl("http://blog.csdn.net/u014231523網址鏈接")
     .contact(new Contact("diaoxingguo", "http://blog.csdn.net/u014231523", "[email protected]"))
     .build();
 
 }
 
}

在原來2.2.0的版本中使用new ApiInfo()的方法已經過時,使用new ApiInfoBuilder()進行構造,需要什么參數就添加什么參數。當然也可以什么都添加。如:

?
1
2
3
private ApiInfo buildApiInfo(){
 return new ApiInfoBuilder().build();
}

那么頁面顯示的效果如圖:

使用new ApiInfoBuilder().build();
Spring Boot集成springfox-swagger2構建restful API的方法教程

添加屬性:

Spring Boot集成springfox-swagger2構建restful API的方法教程

點擊ApiInfoBuilder.Java的源碼可以看到相關方法使用。

第三步就是在自己的controller添加相關的注解:

原來使用在類上使用@controller,現在可以使用@RestController,然后方法的@ResponseBody就可以不用寫了,因為@RestController的注解接口上已經添加了,要求版本在4.0.1之后。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
 
 /**
  * The value may indicate a suggestion for a logical component name,
  * to be turned into a Spring bean in case of an autodetected component.
  * @return the suggested component name, if any
  * @since 4.0.1
  */
 String value() default "";
 
}

常用的注解如下:

      - @Api()用于類名

      - @ApiOperation()用于方法名

      - @ApiParam()用于參數說明

      - @ApiModel()用于實體類

      - @ApiModelProperty用于實體類屬性

更加詳細的含義可以參考官方說明wiki

下面會用代碼和示例圖說明。

第四部就是在啟動項目在瀏覽器上輸入url:

http://{ip}:{port}/swagger-ui.html#/

我在application.properties中設置的自己的端口號為9090(如果不設置,默認為8080)

?
1
server.port=9090

所以我的url是:http://localhost:9090/swagger-ui.html

如圖:

Spring Boot集成springfox-swagger2構建restful API的方法教程

這里會把相應包下的所有controller按類進行顯示。

我們看下其中一個類UserController.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
package com.xingguo.springboot.controller;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
 
import javax.annotation.Resource;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import com.xingguo.springboot.model.User;
import com.xingguo.springboot.service.UserService;
 
/**
 * Created by diaoxingguo on 2016/10/24.
 */
@Api(value="用戶controller",description="用戶操作",tags={"用戶操作接口"})
@RestController
public class UserController {
 
 @Resource
 private UserService userService;
 
 @ApiOperation("獲取用戶信息")
 @GetMapping("/getUserInfo")
 public User getUserInfo(@ApiParam(name="id",value="用戶id",required=true) Long id,@ApiParam(name="username",value="用戶名") String username) {
  User user = userService.getUserInfo();
  return user;
 }
 
 
 @ApiOperation("更改用戶信息")
 @PostMapping("/updateUserInfo")
 public int updateUserInfo(@RequestBody @ApiParam(name="用戶對象",value="傳入json格式",required=true) User user){
  int num = userService.updateUserInfo(user);
  return num;
 }
 
 @ApiOperation("添加用戶信息")
 @PostMapping("/saveUser")
 public String saveUser(@RequestBody @ApiParam(name="user",value="json fromat",required=true) User user) {
  userService.saveUser(user);
  return "success";
 }
}

這里說明下,在使用對象作為參數時,可以在對象上添加相應的注解,用戶頁面顯示。

如:

?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.xingguo.springboot.model;
 
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
 
import java.util.List;
 
/**
 * Created by diaoxingguo on 2016/10/24.
 */
@ApiModel(description="用戶對象user")
public class User {
 @ApiModelProperty(value="用戶名",name="username")
 private String username;
 @ApiModelProperty(value="狀態",name="state",required=true)
 private Integer state;
 private String password;
 private String nickName;
 private Integer isDeleted;
 
 private String[] ids;
 private List<String> idList;
 
 public String getUsername() {
  return username;
 }
 
 public void setUsername(String username) {
  this.username = username;
 }
 
 public Integer getState() {
  return state;
 }
 
 public void setState(Integer state) {
  this.state = state;
 }
 
 public String getPassword() {
  return password;
 }
 
 public void setPassword(String password) {
  this.password = password;
 }
 
 public String[] getIds() {
  return ids;
 }
 
 public void setIds(String[] ids) {
  this.ids = ids;
 }
 
 public List<String> getIdList() {
  return idList;
 }
 
 public void setIdList(List<String> idList) {
  this.idList = idList;
 }
 
 public String getNickName() {
  return nickName;
 }
 
 public void setNickName(String nickName) {
  this.nickName = nickName;
 }
 
 public Integer getIsDeleted() {
  return isDeleted;
 }
 
 public void setIsDeleted(Integer isDeleted) {
  this.isDeleted = isDeleted;
 }
 
 
}

顯示的效果如圖:

Spring Boot集成springfox-swagger2構建restful API的方法教程
Spring Boot集成springfox-swagger2構建restful API的方法教程

看上圖紅框的部分,其中一個是json格式的點擊就可以獲取參數格式。

第二張中可以看到字段相應的注釋和是否必填。

如果在字段上添加注釋@ApiModelProperty(required=true)就是必填(默認是false),相應的頁面optional標識也會消失,標識這個字段必填。

點擊下面的try it out按鈕就可以進行調試。

在使用單個參數時,如上面代碼中的getUserInfo()方法,對應的效果圖如下:

Spring Boot集成springfox-swagger2構建restful API的方法教程

這里如果是添加required=true@ApiParam(required=true)則會在頁面上顯示required的標識。同樣默認為false。

其他的使用方式可以自己動手試試。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://blog.csdn.net/u014231523/article/details/54562695

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产真实伦对白在线播放 | 2022国产麻豆剧传媒剧情 | 精品午夜久久网成年网 | 消息称老熟妇乱视频一区二区 | 四虎4hu新地址入口 四虎1515h永久 | 日本三级大学生17 | 全黄一级裸片视频免费 | 日本国产最新一区二区三区 | 青草草产国视频 | 修修视频在线观看 | 奇米社区 | 性伴交换多p | 色多多绿巨人视频 | 男男同志videos | 国产精品久久久久久久午夜片 | 国产欧美日韩精品一区二区三区 | 国产精品久久久久久久牛牛 | 亚洲精品国产精品国自产观看 | 黄色cc| 亚洲高清网站 | 欧式午夜理伦三级在线观看 | 亚洲国产果果在线播放在线 | 调教催眠 | h片在线看 | 亚洲国产无线码在线观看 | 无人在线视频高清免费观看动漫 | 美女脱了内裤张开腿亲吻男生 | 亚洲精品一区二区三区在线播放 | sao虎影院桃红视频在线观看 | 99rv精品视频在线播放 | 国产日韩欧美在线一区二区三区 | 亚洲精品一二区 | 天天综合色网 | 花唇肿胀无法合拢双性 | 久久综合视频网站 | 青青草视频国产 | 成人亚洲精品一区 | 99re精品在线 | 3d动漫h在线观看网站蜜芽 | 性做久久久久久久久老女人 | 色网在线视频 |