SpringBoot配置Swagger

Swaager 是一个开源用于设计、撰写、测试 RESTful API 的工具,今天学习如何SpringBoot配置Swagger

一、pom.xml中加入jar依赖

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

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<Parameter> 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<VideoVo> 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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值