栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合Swagger2

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合Swagger2

1. 导入依赖
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        

要注意版本问题。一开始我是用3.0版本,访问swagger报404,也没有弹窗,换成2.9.2版本就自动好了。

2. 整合Swagger2
@Configuration   //加入配置类
@EnableSwagger2  //开启swagger2
public class SwaggerConfig {
}

到此,springboot整合swagger2就结束了,现在去浏览器访问http://localhost:8080/swagger-ui.htm就能打开swagger主页面。

还可能遇到一种问题,弹窗问题。

这种是因为SpringBoot屏蔽了静态资源,需要在配置类中加入此段代码

@Configuration
@EnableSwagger2//这里注意,此类要继承WebMvcConfigurationSupport类
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("
    private ApiInfo apiInfo(){
    	//作者信息
        Contact contact=new Contact("Liwz","http://localhost:8081/","979391150@qq.com");

        return new ApiInfo(
                "Liwz的API文档",//title
                "这是用来开发员工管理系统的API文档",//description
                "v1.0",//version
                "http://www.liwz.top",//termsOfServiceUrl
                contact,//contact
                "Apache 2.0",//license
                "http://www.liwz.top",//licenseUrl
                new ArrayList<>()
                 );
    }

访问查看效果。

4. 设置多个Docket

在页面右上角可以看见类似于分组的下拉框

可以继续在配置文件中设置多个Docket,并通过groupName方法,设置在右上角下拉框中显示的名字。

    @Bean
    public Docket docket(){

        return new Docket(documentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("A");
    }

    @Bean
    public Docket docket1(){

        return new Docket(documentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("B");
    }

    @Bean
    public Docket docket2(){

        return new Docket(documentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("C");
    }

查看效果

5. 设置扫描的路径


可以看到默认的配置,扫描了所有的配置,连Springboot的基本error都扫描到了,我们并不需要,可以继续在Config类的具体的Docket中设置。

@Bean
    public Docket docket(){
		
        return new Docket(documentationType.SWAGGER_2).apiInfo(apiInfo())
        .groupName("A")
		.select()
		.apis(RequestHandlerSelectors.basePackage("com.example.springboot.controller"))
		.build()
    }
6. 给Model或者Controller加中文注释

Model:

@ApiModel("用户实体类")
@Data
public class User implements Serializable {

    @ApiModelProperty("用户表主键ID")
    private int userId;
    @ApiModelProperty("账号")
    private String account;
    @ApiModelProperty("用户名")
    private String userName;
    @ApiModelProperty("密码")
    private String password;
    @ApiModelProperty("新密码")
    private String newPwd;
    @ApiModelProperty("性别")
    private int gender;
    @ApiModelProperty("邮箱")
    private String email;
    @ApiModelProperty("电话")
    private String phone;
    @ApiModelProperty("部门")
    private Department department;
    @ApiModelProperty("生日")
    private Date birth;
    @ApiModelProperty("部门Id")
    private int deptId;
    @ApiModelProperty("头像存储路径")
    private String fileName;

}

查看效果

Controller:

@ApiOperation("登录")
    @RequestMapping("/login")
    public Result login(@ApiParam("用户") @RequestBody User user, HttpSession session) {
        User resultUser=userService.login(user);
        if(resultUser!=null){
            String token= UUID.randomUUID()+"";
            Map map=new HashMap();
            tokenUtil.set(token,resultUser, Duration.ofMinutes(30L));
            map.put("token",token);
            map.put("user",resultUser);

            return Result.success(map);
        }else{
            return Result.error("450","用户账号或密码错误!");
        }

    }

效果

7. 使用

在具体的接口右上角,有一个 Try it out按钮

点击一下,在具体的参数列表中输入参数即可。

点击Excute,执行即可。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号