Spring Boot API 自动生成Swagger2使用

1.添加依赖

<!-- swagger2 -->
<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>

2.创建配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kxg.swagger.controller"))
                .build().apiInfo(new ApiInfoBuilder()
                        .description("Swagger 文档")
                        .contact(new Contact("塞北一抹云","","")
                        ).version("v1.0.0")
                        .title("API测试文档")
                        .license("Apache2.0")
                        .licenseUrl("http://www.apache.org/licenses/LICEESE-2.0")
                        .build()
                );
    }
}

 

3.开发接口

@RestController
@Api(tags="用户数据接口")
public class UserController {


    @ApiOperation(value = "查询用户",notes = "根据ID查询用户")
    @ApiImplicitParam(paramType = "path",name = "id",value="用户id",required = true)
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable Integer id)
    {
        return "/user/"+id;
    }

    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")})
    @ApiOperation(value="删除用户",notes = "通过id删除用户")
    @DeleteMapping("/user/{id}")
    public Integer deleteUserByid(@PathVariable Integer id)
    {
        return id;
    }

    @ApiOperation(value = "添加用户",notes = "添加一个用户,传入用户名和地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "username",value="用户名",
                              required = true,defaultValue = "kxg"),
            @ApiImplicitParam(paramType = "query",name = "address",value="用户地址",
                              required = true,defaultValue = "bj")})
    @ApiResponses({
            @ApiResponse(code = 200,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")})
    @PostMapping("/user")
    public String addUser(@RequestParam  String  username, @RequestParam String address)
    {
        return username +":" +address;
    }

    @ApiOperation(value = "修改用户",notes = "修改用户,传入用户信息")
    @PutMapping("/user")
    public String UpdateUser(@RequestBody User user)
    {
        return user.toString();
    }

    @GetMapping("/ignore")
    @ApiIgnore
    public void ignoreMethod()
    {

    }

    @RequestMapping("/")
    @ApiIgnore
    public String test()
    {
        return "test";
    }
}
@ApiModel(value = "用户实体类",description = "用户信息描述类")
@Component
public class User {
    private Integer id;
    private String  name;
    private String  address;

    public String getName() {
        return name;
    }

    public Integer getId() {
        return id;
    }

    public String getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

4.构建项目测试

是与

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

产生了冲突,暂时未能解决(先去除)

5.浏览器查看http://localhost:9090/swagger-ui.html

  • required:表示字段是否必填
  • value:参数的描述信息
  • @ApiOperation(value:对方法的作用的一个描述,notes :备注方法的详细作用)
  • @Api(tags="用户数据接口") 用在类上,用来描述整个类的 信息 
  • paramType 是指方法参数 的类型:path(@PathVariable)、query(@RequestParam)、header(@RequestHeader)、body(@RequestBody)、form
  • name表述参数名称
  • @ApiResponse 对响应结果的描述
  • @RequestBody注解接受数据
  • @ApiModel 和
  • @ApiModelProperty(value="用户名") 注解配置对象的描述信息
  • @ApiIgnore 表示不对某个接口生成 文档

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值