springfox ApiModelProperty position sorting not working

huangapple go评论68阅读模式
英文:

springfox ApiModelProperty position sorting not working

问题

在我的Spring Boot应用程序中,我无法正确管理带有@ApiModel注释的类上字段正确排序的Swagger JSON。

首先,我在我的pom.xml中导入了Springfox库:

<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"
    }
}

这就是你要的翻译,没有其他内容。

英文:

In my spring boot application, I cannot be able to manage my swagger JSON with fields ordered properly on @ApiModel annotated class.

Firstly, I imported springfox lib into my pom.xml:

&lt;dependency&gt;
	&lt;groupId&gt;io.springfox&lt;/groupId&gt;
	&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
	&lt;version&gt;3.0.0&lt;/version&gt;
&lt;/dependency&gt;

Secondly, I created SwaggerConfig.java:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Bean
	public Docket swaggerSpringMvcPlugin() {
		return new Docket(DocumentationType.SWAGGER_2)  
		          .select()                                  
		          .apis(RequestHandlerSelectors.basePackage(&quot;my.package.to.enable.swagger.doc&quot;))           
		          .paths(PathSelectors.any())       
		          .build()
		          .host(&quot;http://localhost:8080&quot;);
	}
}

Thirdly, I created my PersonDTO annotated by @ApiModel:

@ApiModel(value = &quot;Person&quot;, description = &quot;Person entity definition&quot;)
public class PersonDTO {

	@ApiModelProperty(value=&quot;Entity unique ID&quot;, position=0)
	public Long id;

	@ApiModelProperty(value=&quot;Person&#39;s number, useful to provide a convenient way to quickly communicate a person&#39;s reference&quot;, position=1)
	public Integer number;
	
	@ApiModelProperty(value=&quot;Person&#39;s first name&quot;, position=2)
	public String firstName;
	
	@ApiModelProperty(value=&quot;Person&#39;s last name&quot;, position=3)
	public String lastName;
	
}

Then, when I requesting for Json output at url http://localhost:[port]/[servlet-context-path]/v2/api-docs, it seems that the position order is not working:

&quot;definitions&quot;: {
	&quot;Person&quot;: {
	  &quot;type&quot;: &quot;object&quot;,
	  &quot;properties&quot;: {
		&quot;firstName&quot;: {
		  &quot;type&quot;: &quot;string&quot;,
		  &quot;description&quot;: &quot;Person&#39;s first name&quot;
		},
		&quot;id&quot;: {
		  &quot;type&quot;: &quot;integer&quot;,
		  &quot;format&quot;: &quot;int64&quot;,
		  &quot;description&quot;: &quot;Entity unique ID&quot;
		},
		&quot;lastName&quot;: {
		  &quot;type&quot;: &quot;string&quot;,
		  &quot;description&quot;: &quot;Person&#39;s last name&quot;
		},
		&quot;number&quot;: {
		  &quot;type&quot;: &quot;integer&quot;,
		  &quot;format&quot;: &quot;int32&quot;,
		  &quot;description&quot;: &quot;Person&#39;s number, useful to provide a convenient way to quickly communicate a person&#39;s reference&quot;
		}
	  },
	  &quot;title&quot;: &quot;Person&quot;,
	  &quot;description&quot;: &quot;Person entity definition&quot;
	}
}

答案1

得分: 4

这在3.0.0版中目前存在问题,请参见 https://github.com/springfox/springfox/issues/3391

英文:

This is currently broken in 3.0.0 see https://github.com/springfox/springfox/issues/3391

答案2

得分: 0

使用 @RequestBody 在控制器方法中操作。

英文:

Use @RequestBody works in controller method

huangapple
  • 本文由 发表于 2020年8月27日 21:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63616811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定