swagger2 使用示例


swagger2 使用示例

 

 

*****************************

示例

 

*****************

pojo 层

 

Person

@Data
@ApiModel(value = "用户")
public class Person {

    @ApiModelProperty(value = "用户 id",required = true)
    private Integer id;

    @ApiModelProperty(value = "用户名称",required = true)
    private String name;

    @ApiModelProperty(value = "用户年龄",required = true)
    private Integer age;
}

 

*****************

config 层

 

Swagger2Config

@Configuration
@EnableOpenApi
public class Swagger2Config {

    @Bean
    public Docket initDocket(){
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(initApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.ant("/*"))
                .build();
    }

    private ApiInfo initApiInfo(){
        return new ApiInfoBuilder()
                .title("构建 swagger2")
                .description("hello world")
                .termsOfServiceUrl("http://localhost:8080/hello")
                .version("v1.0")
                .build();
    }
}

说明:使用springfox-boot-starter,@EnableOpenApi可省略

 

*****************

controller 层

 

HelloController

@RestController
@Api(value = "hello 控制器")
public class HelloController {

    @RequestMapping("/hello")
    @ApiOperation(value = "hello 方法")
    @ApiResponse(code = 200,message = "处理成功")
    public String hello(){
        return "hello 瓜田李下";
    }

    @RequestMapping("/hello2")
    @ApiOperation(value = "hello2 方法")
    public String hello2(@ApiParam(value = "person",required = true) Person person){
        System.out.println(person);

        return "hello 瓜田李下2";
    }

    @RequestMapping("/hello3")
    @ApiOperation(value = "hello3 方法")
    @ApiImplicitParam(name = "param",value = "param 参数")
    public String hello3(String param){
        System.out.println(param);

        return "hello 瓜田李下3";
    }
}

 

 

*****************************

使用测试

 

http://localhost:8080/swagger-ui/index.html

       

 

控制器方法

       

      

get ==> /hello2

       

       

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值