Swaager 是一个开源用于设计、撰写、测试 RESTful API 的工具,今天学习如何SpringBoot配置Swagger
一、pom.xml中加入jar依赖
io.springfox
springfox-swagger-ui
2.9.2
io.springfox
springfox-swagger2
2.9.2
2、Swagger配置注入
启动类加入EnableSagger2, restful风格ApiInfo注入
@EnableSwagger2
public class GoodsApplication {
public static void main(String[] args) {
SpringApplication.run(GoodsApplication.class, args);
}
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List pars = new ArrayList<>();
tokenPar.name("token")
.defaultValue("123456")
.description("令牌")
.modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.fcw.app.goods.api"))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars)
.apiInfo(apiInfo());
}
@Bean
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("API Title")
.description("API Description")
.termsOfServiceUrl(" API terms of service")
.version("1.0.0")
.build();
}
}
3、PO、VO、Controller加入注解
@Data
@Api(value = "VideoVo",description = "视频信息类")
public class VideoPo implements Serializable {
@ApiModelProperty(notes = "逻辑主键")
private int id;
@ApiModelProperty(notes = "业务主键")
private String videoId;
@ApiModelProperty(notes = "视频名称")
private String videoName;
@ApiModelProperty(notes = "视频播放地址")
private String videoUrl;
@ApiModelProperty(notes = "视频内容")
private String videoContent;
@ApiModelProperty(notes = "视频缩略图")
private String videoThumb;
@ApiModelProperty(notes = "作者id")
private String userId;
}
@Controller
@RequestMapping("/video/")
@Api(description = "在线视频")
public class VideoController extends BaseController {
private final IVideoService videoService;
@Autowired
public VideoController(IVideoService videoService) {
this.videoService = videoService;
}
@ApiOperation("查询在线视频列表")
@ApiImplicitParams({@ApiImplicitParam(name = "page",value = "分页页码",required = true,dataType = "Integer",paramType = "query"),@ApiImplicitParam(name = "videoName",
value = "视频名称",dataType = "String")})
@ApiResponses({
@ApiResponse(code = 200, message = "请求成功", response = BasePage.class),
@ApiResponse(code = 400, message = "请求参数错误", response = BasePage.class),
@ApiResponse(code = 401, message = "未授权的访问", response = BasePage.class),
@ApiResponse(code = 403, message = "拒绝访问", response = BasePage.class),
@ApiResponse(code = 404, message = "资源不存在", response = BasePage.class),
@ApiResponse(code = 500, message = "服务器内部错误", response = BasePage.class)
})
@RequestMapping(value = "getVideoList",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@JsonInclude(JsonInclude.Include.NON_NULL)
public BasePage getVideoList(@RequestParam("page") int page,String videoName) {
return videoService.findVideoList(page,videoName);
}
}
4、服务的根url+swagger-ui.html直接访问即可localhost:8086/swagger-ui.html
Swagger 常用注解说明:https://github.com/helloworlde/SpringBootCollection/blob/master/SpringBoot-Swagger/Swagger.md