springboot 集成swagger
swagger
作用:自动生成后端restful 接口的api
springboot集成
1.依赖
<!--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>
2.配置文件
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.poc.demo"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("poc_RESTful APIs")
.description("服务名:poc")
.version("1.0")
.build();
}
}
3.引入使用
ctrl类上添加
//主要参数tags和description,tags说明该类的作用,非空时将覆盖value的值 ,description 描述
@Api(tags = "CPK", description = "cpk数据采集和报表")
方法上添加
//主要参数value和notes, 显示value 说明方法作用
@ApiOperation(value = "新增cpk")
@ApiModelProperty
//描述实体类的属性,一般适用于post方法传对象时的描述,如下:
@ApiModelProperty
private int id;
@ApiModelProperty(value="姓名")
private String name;
@ApiModelProperty(value="密码")
private int age;
@ApiParam: 用在请求方法中,描述参数信息
void Hello(@ApiParam(name = "name", value = "用户名")String name)
4.访问页面
http://localhost:port/swagger-ui.html
其他:出现异常
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_191]
at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_191]
at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_191]
解决:Integer类型加入 example
@ApiModelProperty(value = "id",example = "123")