创建工程
创建一个Spring Boot项目,加入web和两个Swagger2相关的依赖如下:
<dependency
>
<groupId
>io
.springfox
</groupId
>
<artifactId
>springfox
-swagger2
</artifactId
>
<version
>2.9.2</version
>
</dependency
>
<dependency
>
<groupId
>io
.springfox
</groupId
>
<artifactId
>springfox
-swagger
-ui
</artifactId
>
<version
>2.9.2</version
>
</dependency
>
<dependency
>
<groupId
>org
.springframework
.boot
</groupId
>
<artifactId
>spring
-boot
-starter
-web
</artifactId
>
</dependency
>
创建swagger启动类
package com
.example
.text
.config
;
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
.spi
.DocumentationType
;
import springfox
.documentation
.spring
.web
.plugins
.Docket
;
import springfox
.documentation
.swagger2
.annotations
.EnableSwagger2
;
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
@Bean
public Docket
createRestApi() {
return new
Docket(DocumentationType
.SWAGGER_2
)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.basePackage("com.example.text.controller"))
.paths(PathSelectors
.any())
.build();
}
private ApiInfo
apiInfo() {
return new
ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("hello swagger")
.version("1.0")
.build();
}
}
访问http://localhost:8080/swagger-ui.html#/ 出现这个说明成功
创建接口
package com
.example
.text
.controller
;
import io
.swagger
.annotations
.*;
import org
.springframework
.web
.bind
.annotation
.*;
import springfox
.documentation
.annotations
.ApiIgnore
;
import java
.util
.ArrayList
;
import java
.util
.HashMap
;
import java
.util
.List
;
import java
.util
.Map
;
@RestController
@
Api(tags
= "Swagger测试接口")
@
RequestMapping("/c/")
public class TextSwaggerController
{
@
GetMapping(value
= "/queryAll")
@
ApiOperation(value
= "查询所有信息",notes
= "查询数据库中某个学生的信息")
@
ApiImplicitParams({
@
ApiImplicitParam(name
= "name",value
= "用户名",defaultValue
= "王五",required
= true
),
@
ApiImplicitParam(name
= "comment",value
= "备注",defaultValue
= "奥里给给")
})
public List
<String
> queryAll(
@
RequestParam(required
= true
) String name
,String comment
) throws Exception
{
List
<String
> data
=new ArrayList
<String
>();
data
.add("java134");
data
.add("Java资料社区");
return data
;
}
@
ApiOperation(value
="获取redis中数据", notes
="通过key值获取")
@
ApiResponses({
@
ApiResponse(code
=400,message
= "请求报文存在语法错误"),
@
ApiResponse(code
=401,message
= "发送的请求需要有通过 HTTP 认证的认证信息"),
@
ApiResponse(code
=403,message
= "请求资源的访问被服务器拒绝"),
@
ApiResponse(code
=404,message
="服务器上没有找到请求的资源")
})
@
ApiImplicitParam(name
= "key", value
= "redis键值", required
= true
, dataType
= "String")
@
PostMapping(value
= "/getRedis")
public String
getRedis(String key
) throws Exception
{
Map
<String
,String
> data
=new HashMap
<String
,String
>();
data
.put("123","Java资料社区");
data
.put("321","Java");
data
.put("132","小编");
return data
.get(key
);
}
@ApiIgnore
@
RequestMapping("/index")
public String
index() {
return "ApiIgnore=忽略这个接口";
}
@ApiImplicitParam注解中虽然可以指定参数是必填的,但是却不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架内必填,离开了Swagger2,这个限制就没用了,所以假如开发者需要指定一个参数必填,@RequestParam(required = true)注解还是不能省略。
如果参数是一个对象,参数描述也可在对象中
@ApiModel
public class User
{
@
ApiModelProperty(value
= "用户id")
private Integer id
;
}
写好以后从新刷新刚刚的网页,会发现所有的controller层的接口都展现出来
利用Swagger做接口测试
可以点击右上角的Try it out,就可以进行接口测试了 这里的name是传递的参数 已经有结果了呀 完美谢幕