springboot-restful api-swagger2 ui-实战
2017-08-22 15:40
471 查看
swagger2 ui 自动生成api文档,就节省了编写接口测试和文档的工作,非常方便
下面以集成融云IM api 为例,展示如何配置swagger2
pom.xml
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
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
application.properties
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
SwaggerConfig
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Application
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
controller
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
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
访问
http://localhost:9090/swagger-ui.html
效果
还可以填写http参数 进行测试
下面以集成融云IM api 为例,展示如何配置swagger2
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency> <!-- 7 devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现), 8 实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。 9 即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的 10 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 --> </dependency> </dependencies>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
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
application.properties
#项目contextPath,一般在正式发布版本中,我们不配置 #server.context-path=/springboot # 错误页:指定发生错误时,跳转的URL。请查看BasicErrorController。 #server.error.path=/error # 服务端口,默认为8080 server.port=9090 # session最大超时时间(分钟),默认为30 server.session-timeout=60 # 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置 #server.address=192.168.1.1121
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
SwaggerConfig
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { ApiInfo apiInfo = new ApiInfoBuilder() .title("ShiQiqao_YiY_即时通信API") .description("客户端与服务端接口文档") .termsOfServiceUrl("http://localost:8080") .contact("xiaoyuzhou") .version("1.0.0") .build(); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .select() .apis(RequestHandlerSelectors.basePackage("com.vc.im.rongcloud.api.web.controller")) .paths(PathSelectors.any()) .build(); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Application
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
controller
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import java.util.Collections; import java.util.HashMap; import java.util.Map; @RestController @CrossOrigin @RequestMapping(value = "/im") public class ApiController { // 创建线程安全的Map static RongCloud rongCloud = RongCloud.getInstance(AppConstants.APPKEY, AppConstants.APPSECRET); // ************************User******************** @ApiOperation(value = "获取Token", notes = "") @ApiImplicitParams(value = { @ApiImplicitParam(name = "userId", value = "用户Id", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "portraitUri", value = "用户头像地址", required = true, paramType = "query", dataType = "String") }) @RequestMapping(value = {"/user/token"}, method = RequestMethod.GET) public String getToken(@RequestParam String userId,@RequestParam String name,@RequestParam String portraitUri) throws Exception { TokenResult userGetTokenResult = rongCloud.user.getToken(userId, name, portraitUri); return userGetTokenResult.toString(); } //... }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
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
访问
http://localhost:9090/swagger-ui.html
效果
还可以填写http参数 进行测试
相关文章推荐
- springboot-restful api-swagger2 ui-实战
- springboot-restful api-swagger2 ui-实战
- SpringBoot实践之---集成Swagger2生成Restful风格的在线API
- SpringBoot实战之12 整合restful工具swagger2
- Springboot中使用Swagger2构建RESTful API文档
- springboot集成swagger2构建RESTful API文档
- Spring Boot 2.0.0.M7 中使用Swagger2构建RESTful API文档
- springboot集成swagger2构建RESTful API文档
- Spring boot 中使用swagger-ui实现 restful-api
- 在spring-boot中使用swagger2来构建RESTful API文档
- springboot集成swagger2,构建优雅的Restful API doc
- Spring Boot实战:Restful API的构建
- Spring Boot构建RESTful API与单元测试
- spring boot+swagger 生成Restful风格API
- 使用 JSONDoc 记录 Spring Boot RESTful API
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
- SwaggerUI+SpringMVC——构建RestFulAPI的可视化界面
- SpringBoot使用教程【1】Restful API设计 返回json,xml格式
- SwaggerUI+SpringMVC——构建RestFul API的可视化界面
- Spring Boot构建RESTful API与单元测试