是否可以使用Swagger通过Jersey中的
MultivaluedMap
参数注释来制作API文档?
我有一小段这样的代码:
/**
* Method which serves requests of adding {@link StudentGroup} to DB
*
* @param name
* @param description
* @return {@link Response}
* @throws RestServiceException
*/
@POST
@Path("/add")
public Response addStudentGroup(MultivaluedMap<String, String> formParams) throws
RestServiceException {
String name = formParams.getFirst("name");
String description = formParams.getFirst("description");
String studentIds = formParams.getFirst("studentIds");
(...)
}
并且我想使用
@ApiParam
使用 Swagger 和 Swagger UI 生成带有文档数据的 JSON
。
如果我将
@ApiParam
放在 MultivaluedMap<String, String>
formParams 之前,它不起作用。 Swagger 无法列出任何参数。
这似乎是 Swagger 中的一个错误 - 我也遇到了这种行为。使用具有两个类型参数的其他泛型类(如 @ApiParam() HashMap)效果很好。可能它会导致解析器关闭。
我在 Swagger 错误跟踪系统上打开了为此问题。
您还可以在他们的 Google 群组中询问他们,或者在 IRC 上的 Freenode#swagger 上找到他们。
我可以这样解决这个问题。我们可以利用 Swagger API 提供的 @APIImplicitParams 注解来列出 MultiValueMap 的所有表单元素,并在 MultiValueMap 本身上使用 @APIignore。尝试一下下面的代码行是否有效。祝你好运。 :)
@ApiOperation(value = "Create a new 'Student' object")
@ApiImplicitParams({
@ApiImplicitParam(name = "X-ClientId", value = "X-ClientId", required = true, dataType = "String", paramType = "header"),
@ApiImplicitParam(name = "id", value = "Enter an ID for the student.", dataTypeClass = String.class, paramType = "query"),
@ApiImplicitParam(name = "name", value = "Enter a Name for the Student.", dataTypeClass = String.class, paramType = "query")
})
ResponseEntity addStudent(
@NotBlank @RequestHeader(value = X_CLIENTID) String headerXclient,
@ApiIgnore @RequestParam MultiValueMap<String, String> requestParams) {
StudentRequest request = new StudentRequest(requestParams);
(... Your Code Implementation ...)
};
目前不支持,但正如 Eyal 所说,在 github issues 中有一个开放的票证,它可能相当容易实现。