自动生成Api文档工具Swagger

参考文章:

  1. https://symonlin.github.io/2019/02/02/springboot-3/
  2. https://blog.csdn.net/u012373815/article/details/82685962 (较详细,两篇都记得修改apis为你的controller目录)

首先引入相关的依赖:

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

接着修改配置文件:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.Controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://localhost:8080/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("描述")
                .termsOfServiceUrl("。。。")
                .contact("abel")
                .version("1.0")
                .build();
    }

}

接着是使用的一些注解和使用方法(尝试一下基本就知道了)

@Api:用在类上,说明该类的作用。

@ApiOperation:注解来给API增加方法说明。

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

@ApiImplicitParam:用来注解来给方法入参增加说明。

@ApiResponses:用于表示一组响应

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

  code:数字,例如400

  message:信息,例如"请求参数没填好"

  response:抛出异常的类   

@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性

使用例子:

  1. controller类
@Api("登陆相关的接口 相关接口说明")
//返回的错误类型
@ApiResponses({
        @ApiResponse(code = 200, message = "OK"),
        @ApiResponse(code = 400, message = "客户端请求错误"),
        @ApiResponse(code = 404, message = "找不到路径"),
        @ApiResponse(code = 500, message = "编译异常")
})

public class login {
    @Autowired
    private UserService userService;

    @RequestMapping("toLogin")
    //描述某个接口
    @ApiOperation(value = "跳转到登陆界面",httpMethod = "GET", notes="无需传值")
    public ModelAndView toLogin(){
        return new ModelAndView("login");
    }

    @RequestMapping("login")
    @ApiOperation(value = "登陆",httpMethod = "POST")
    public ModelAndView login(User user){
        User user1=userService.selectByPrimaryKey(user.getUsername());
        if(user.getPassword().equals(user1.getPassword())){
            return new ModelAndView("success");
        }
        else {
            return new ModelAndView("fail");
        }
    }
}

  1. model类
@ApiModel(value = "User", description = "用户信息描述")
public class User {
    @ApiModelProperty("姓名")
    private String username;
}

最后只需要访问localhost http://localhost:8080/swagger-ui.html

git地址:https://github.com/jiaojiaoyow/Auto_ApiFile.git

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值