我正在尝试将我的Spring MVC应用程序与Swagger集成,以使REST API文档保持清洁和最新 . 但是我遇到了一个在swagger UI中表示java Map的问题 . 这是我的简单控制器和型号:

  • 用户
@Data
@ApiModel
public class User {

  @ApiModelProperty(example = "test@example.com")
  private String email;

  @ApiModelProperty(example = "John")
  private String firstName;

  @ApiModelProperty(example = "Doe")
  private String lastName;

  @ApiModelProperty
  private Map<String, Address> addresses;

}
  • 地址
@Data
@ApiModel
public class Address {

  @ApiModelProperty(example = "Calle 39 No 1540")
  private String street;

  @ApiModelProperty(example = "B1000TBU")
  private String postalCode;

  @ApiModelProperty(example = "San Sebastian")
  private String city;

  @ApiModelProperty(example = "Argentina")
  private String country;

}
  • 用户控制器
@RestController
@Api(value = "User Management")
public class UserController {

  @ApiOperation("Get a user profile by the email")
  @RequestMapping(value = "/user/{email:.+}", method = RequestMethod.GET)
  public User getPersonByFirstName(@ApiParam @PathVariable("email") String email) {
    return new User();
  }
}

为了生成Swagger规范,我使用Swagger Maven插件 . 在和我得到下一个文件:

enter image description here

而不是正确的类型 - Map[String, Address] - 我得到了奇怪的inline_model . 在选项卡示例值上,地址的值只是空对象 - {} .

我尝试指定此Map字段的数据类型,但以下注释中的任何一个都不起作用:

  • @ApiModelProperty(value =“addresses”,dataType =“Map [string,com.redkite.model.Address]”)

  • @ApiModelProperty(value =“addresses”,dataType =“java.util.Map”)

  • @ApiModelProperty(value =“addresses”,dataType =“java.util.Map <java.lang.String,com.redkite.model.Address>”)

另外,我试着在related question找到答案,但它并没有解决我的问题 .

问题是 - Swagger UI是否支持Java Map?如果不是 - 是否有一些解决方法可以解决这个问题?

我使用下一版本的框架和插件:

  • Swagger Maven插件 - 3.1.3(生成2.0版规范)

  • Swagger-annotations - 1.5.9

  • Swagger UI - 2.2.10

有关更多信息,请附上generated specification .