帖子摘要:SpringBoot使用Swagger2 1.引入swagger依赖2.添加swagger配置类3.测试Controller4.测试5.swagger的注解Api注解ApiOperation注解Api......
6 w( ^5 O1 x/ j3 }1 Z c+ D
8 s: u) \7 V# R! t, K大家好,欢迎来到Java吧(www.java8.com),交流、学习Java技术、获取Java资源无任何套路,今天说一说:“SpringBoot使用Swagger2”
9 f& ~: D7 l0 D$ P9 Z" k& [7 D1 x2 O
# J" W( Y7 T' j0 I( s3 C( H% k& n2 m7 d) J R, W- L; ]5 N
( o+ A0 P3 Z s y$ F; }0 U& ^ ' Y$ Q8 t6 a( Q0 n
3 y2 e8 {, o! ]- Y6 \
5 r( ]1 t" W1 b$ v+ C
) Y$ {, c$ f- r# [2 E8 E' b " s$ e u" d% ?7 Z
SpringBoot使用Swagger2
+ j' U. W" y% j3 h& `& z+ u& [ 1.引入swagger依赖2.添加swagger配置类3.测试Controller4.测试5.swagger的注解Api注解ApiOperation注解ApiImplicitParam、ApiImplicitParams注解ApiParam注解ApiResponse、ApiResponses注解ResponseHeader注解ApiModel、ApiModelProperty注解
5 t- I6 l" O: t' A8 T3 j; O( l 6.更多
% p; k$ b7 `4 x! H$ G J+ m - \) [5 L3 w$ x, X
1.引入swagger依赖
5 F' l4 g2 g( T2 o+ P
-
5 x1 c% l( O. A
- io.springfox
- springfox-swagger2
- 2.9.2
-
-
% g, b3 [6 o6 f) G# U' }) I q8 {# G
- io.springfox
- springfox-swagger-ui
- 2.9.2
9 ^ P6 @8 x8 r6 Q9 C! c
-
复制代码
3 t9 g" @. t% O2 j3 A6 q5 e
2.添加swagger配置类 ; L+ Q4 U1 A3 I+ e
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
! z2 ?6 y+ o% Q1 j8 j
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.PathSelectors;
- import springfox.documentation.builders.RequestHandlerSelectors;
2 D `* d4 V# h6 e* W7 @
- import springfox.documentation.service.ApiInfo;
2 C, {# n3 C) q( R j3 v; n+ G
- import springfox.documentation.spi.DocumentationType;
0 i2 Y6 U5 ?$ b: r) D
- import springfox.documentation.spring.web.plugins.Docket;
: k3 S/ e" b0 ~" h* G! J6 \0 A4 P6 g
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
& s+ I0 U0 }0 e5 K# b0 a7 g
- @Configuration
$ K0 S; C9 u% E' v
- @EnableSwagger2
- public class SwaggerConfig {
0 N5 A1 P- L/ @1 m* W/ C* s! G
- @Bean
- public Docket createRestApi(){
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
-
. V: I+ G; P; a6 ^
- //swagger文档扫描的包这里扫描的是全部
/ G% j6 T( R$ C5 t; r1 H b
- //如果扫描指定包下的可以这样写
4 A, p% y; h7 I5 R+ ^2 X
- //.apis(RequestHandlerSelectors.basePackage("com.xxx.yyy.controller"))
- .apis(RequestHandlerSelectors.any())
. g3 Q( K( v6 Y. p
- .paths(PathSelectors.any())
- .build();
- }
8 X5 y& H- n! ~3 A
- private ApiInfo apiInfo(){
+ {. `: z* i# s7 i* M
- return new ApiInfoBuilder()
( g, ]/ j" o7 c W) w
- .title("标题")
0 i% M# X5 p4 ]8 b, v: z8 X. a) X" H4 C/ ~
- .description("描述")
- .version("版本")
% y) a4 j2 X; E( T* d6 t \+ P4 v
- .termsOfServiceUrl("公司网址")
- .build();
) ?. k" A' E+ f9 F1 r, |
- }
2 b; S1 U9 Q7 x- a2 S' M
- }
+ |+ ]+ Q, g9 w: a2 e7 Z
复制代码
4 f5 g8 F2 f9 U, n- q
3.测试Controller 7 F3 u. c. Z! m1 |, T% K% S& e
- @RestController
- @RequestMapping("/test")
- public class TestController {
2 D1 b' U- Q- D
- @GetMapping("/test01")
) Z3 f h8 b7 D
- public String test01(){
- return "test01";
- }
+ v* v' N3 l5 Q* Q& b% y' p* Z% v
- @GetMapping("/test02")
8 Q! l% e$ ~! U5 @ Z0 y c
- public String test02(){
* ~9 ^2 T2 j! p6 n( Q
- return "test02";
- }
- @GetMapping("/test03")
5 l: F4 b6 H5 s8 R! G
- public String test03(){
- return "test03";
& K& K K# o: |# C9 }
- }
- }
2 S# y" v, q2 ~6 J* I- p
复制代码
& K4 @5 w* T) n. z' v
4.测试
( K2 ~3 R) @; `( d' V" ?项目正常启动后浏览器输入网址
7 t* `7 S5 @' g9 T6 A9 R7 N http://localhost:8100/swagger-ui.html#/
( l" _/ X9 P. Z# N
4 S! [8 x0 D& a) a[color=]这里的端口填写自己服务的端口我的是8100默认端口80800 X7 s: |4 K# ]' t( X* {
0 m/ F5 z! ]- ^7 P! G
|