欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

解决springboot引入swagger2不生效问题,

来源: javaer 分享于  点击 14408 次 点评:145

解决springboot引入swagger2不生效问题,


目录
  • 问题描述:
  • springboot引入swagger2的步骤:
    • ①引入依赖
    • ②编写Swagger2的配置类
    • ③在controller中添加注解:按需添加注解
    • ④在model(pojo)上加注解,按需添加
  • 一些注解的使用

    今天遇到跟同事遇到一个由于失误导致的问题,也可以说比较难发现了.在此记录一下(我们用的springboot是2.0.3,swagger是2.2.2)

    问题描述:

    swagger修改title,description等都不生效。并且启动springboot,没有有去加载swagger的配置类。(在debug模式启动)

    经过不断的查找,发现了原因是:swagger的配置类的注解加错了。@Configuration不小心写成了@Configurable.

    还有就是@EnableSwagger2注解只需要加在swagger配置类上

    springboot引入swagger2的步骤:

    ①引入依赖

    <!--  引入swagger包 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.2.2</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.2.2</version>
            </dependency>
    

    ②编写Swagger2的配置类

    @Configuration
    @EnableSwagger2
    public class Swagger2Config {
        @Bean
        public Docket api(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(getApiInfo())
                    .select()
                   .apis(RequestHandlerSelectors.basePackage("com.xx.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
        private ApiInfo getApiInfo(){
            return new ApiInfoBuilder()
                    .title("Swagger2....")
                    .description("Swagger2")
                    .version("1.0")
                    .license("Apache 2.0")
                    .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                    .build();
        }
    }
    

    ③在controller中添加注解:按需添加注解

    @Controller
    @RequestMapping("/user")
    @Api(tags = "我的接口模块")
    public class UserController {
        @Autowired
        private UserService userService;
    	//注意这个注解跟请求对应的@XxxMapping,要不然这个接口会生成好多方法
        @GetMapping(value = "/getUserById")
        @ResponseBody
        @ApiOperation(value = "根据ID查询User")
        public User getUserById(@RequestParam(value = "id") int id){
            return userService.getUserById(id);
        }
    }
    

    ④在model(pojo)上加注解,按需添加

    @ApiModel(value = "用户对象")
    public class User {
        @ApiModelProperty(value = "用户ID", name = "userId")
        private Integer userId;
        @ApiModelProperty(value = "用户姓名",name = "userName")
        private String userName;
        @ApiModelProperty(value = "用户密码",name = "password")
        private String password;
        @ApiModelProperty(value = "用户手机号",name = "phone")
        private String phone;
    

    一些注解的使用

    @Api:一般用于Controller中,用于接口分组

    @ApiOperation:接口说明,用于api方法上。

    @ApiImplicitParams:用在方法上包含一组参数说明

    @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面

    paramType:参数放在哪个地方

    header 请求参数的获取:@RequestHeader

    query 请求参数的获取:@RequestParam

    path(用于restful接口) 请求参数的获取:@PathVariable

    body(不常用)

    form(不常用)

    name:参数名

    dataType:参数类型

    required:参数是否必须传

    value:参数的意思

    defaultValue:参数的默认值

    @ApiResponses:用于表示一组响应

    @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息

    code:数字,例如400

    message:信息&#xff0c;例如”请求参数没填好”

    response:抛出异常的类

    @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)表明这是一个被swagger框架管理的model,用于class上

    @ApiModelProperty :使用在实体类上的成员变量上,描述成员变量的含义。

    以上就是解决springboot引入swagger2不生效问题的详细内容,更多关于springboot引入swagger2的资料请关注3672js教程其它相关文章!

    您可能感兴趣的文章:
    • springboot详解整合swagger方案
    • springBoot详解集成Swagger流程
    • springboot swagger不显示接口的问题及解决
    • SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题
    • Springboot2.6.x高版本与Swagger2版本冲突问题解决方法
    • Springboot整合Swagger3全注解配置(springdoc-openapi-ui)
    • Springboot配置Swagger2登录密码的实现
    • SpringBoot使用Swagger范例讲解
    相关栏目:

    用户点评