如何解决springfox ApiModelProperty位置排序不起作用
在我的Spring Boot应用程序中,我无法使用在@ApiModel
带注释的类上正确排序的字段来管理我的swagger JSON。
首先,我将springfox lib导入到pom.xml中:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
第二,我创建了SwaggerConfig.java:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("my.package.to.enable.swagger.doc"))
.paths(PathSelectors.any())
.build()
.host("http://localhost:8080");
}
}
第三,我创建了由@ApiModel
注释的PersonDTO:
@ApiModel(value = "Person",description = "Person entity definition")
public class PersonDTO {
@ApiModelProperty(value="Entity unique ID",position=0)
public Long id;
@ApiModelProperty(value="Person's number,useful to provide a convenient way to quickly communicate a person's reference",position=1)
public Integer number;
@ApiModelProperty(value="Person's first name",position=2)
public String firstName;
@ApiModelProperty(value="Person's last name",position=3)
public String lastName;
}
然后,当我在URL http://localhost:[port]/[servlet-context-path]/v2/api-docs
请求Json输出时,排名顺序似乎无效:
"definitions": {
"Person": {
"type": "object","properties": {
"firstName": {
"type": "string","description": "Person's first name"
},"id": {
"type": "integer","format": "int64","description": "Entity unique ID"
},"lastName": {
"type": "string","description": "Person's last name"
},"number": {
"type": "integer","format": "int32","description": "Person's number,useful to provide a convenient way to quickly communicate a person's reference"
}
},"title": "Person","description": "Person entity definition"
}
}
解决方法
当前在3.0.0中已被破坏,请参见https://github.com/springfox/springfox/issues/3391
,在控制器方法中使用@RequestBody 工作
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。