2015-03-30 4833 views
6

我正在使用Spring MVC(通过Spring Boot)并使用swagger-spring-mvc库集成了Swagger API文档。如何将@ApiModelProperty dataType设置为Swagger文档的字符串

我有一个类,它看起来是这样的:

@ApiModel 
public class CartItem { 
    ... 
    private Money listPrice; // joda money class 

    @JsonSerialize(using = ToStringSerializer.class) 
    @ApiModelProperty(required = true, dataType = "java.lang.String") 
    public Money getListPrice() { 
     return listPrice; 
    } 
    ... 
} 

由于我使用的是ToStringSerializer这个领域,它返回的JSON listPrice.toString,换句话说:

{ 
    "listPrice": "USD 10.50" 
} 

但是,swagger文档不符合dataType =“java.lang.String”。这表明作为响应模型:

"CartItem": { 
    "description": "", 
    "id": "CartItem", 
    "properties": { 
     "listPrice": { 
      "required": false, 
      "type": "Money" 
     } 
    } 
} 

我试图把@ApiModelProperty注释场上以及方法,并在这两种情况下required场得到尊重,但dataType场被忽略。我也尝试过使用“字符串”,“字符串”和“java.lang.String”作为数据类型,但都没有工作。

我错过了什么,或者这只是swagger-spring-mvc库中的一个错误?

回答

4

原来在Swagger Spring MVC库的当前版本中完全忽略了dataType。我发现一个简短的讨论在这里:

https://github.com/springfox/springfox/issues/602

看起来可能包含在第2版一旦超出。

编辑:尽管版本2表示它支持dataType,但目前似乎没有工作。我需要一个更好的方法是用这样的直销模式替代配置文件设置:

@Bean 
public Docket swaggerSpringMvcPlugin() { 
    return new Docket(DocumentationType.SWAGGER_2) 
      .directModelSubstitute(Money.class, String.class); 
} 
+1

你是正确的,它在2.0.x版本现在支持(在2.0.0-SNAPSHOT可用),有一些注意事项。数据类型需要是有效的包合格类。 – 2015-03-31 23:54:44

+0

谢谢@DilipKrishnan。我看到github上的文档已经更新以反映更改。 – nerdherd 2015-04-01 00:37:14