Swagger使用

swagger生成api文档,先看一下效果


swagger 对于userController的接口的描述

1.使用

1.1pom引入

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

1.2 @EnableSwagger2

@SpringBootApplication
@EnableSwagger2
public class DemoApplication {

    public static void main(String[] args){
        SpringApplication.run(DemoApplication.class,args);
    }
}

1.3 用中文进行详细描述

1.3.1 对接口进行描述

  /**
     * 在url中使用正则表达式
     * @param id
     * @return
     */
    @GetMapping("/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    @ApiOperation(value = "获取用户详情")
    public User get(@ApiParam(value = "用户id") @PathVariable String id){
        //throw new UserNotFoundException(id);
        System.out.println(id);
        User user = new User();
        user.setUsername("tom");
        return user;
    }
  • 使用@ApiOperation(value = "获取用户详情")对接口进行描述

1.3.2 对字段进行描述

  • 使用@ApiModelProperty进行描述
public class UserQueryCondition {

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "年龄起始值")
    private int age;

    @ApiModelProperty(value = "年龄终止值")
    private int ageTo;
  • 使用@ApiParam进行描述
@DeleteMapping("/{id:\\d+}")
    @ApiOperation(value = "删除用户信息")
    public void delete(@ApiParam(value = "用户Id") @PathVariable String id){
    }

2.访问

  • 访问端点/swagger-ui.html
    例如:http://localhost:8080/swagger-ui.html#/
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容