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