【发布时间】:2020-10-16 07:40:37
【问题描述】:
在我的 REST API PATCH 操作中,我使用的是 v3 swagger-annotation:2.0.6
我试图为我的补丁操作PATCH /users/id 添加更多示例作为招摇模式。
请求正文:
{
"operationList": [
{
"op": "replace",
"path": "/username",
"value": "john1991"
}
]
}
目前我有以下请求模型类。
public class UserPatchOp extends PatchOperation {
@Override
@Schema(description = "some description", example = "replace", required = true)
public PatchOperator getOp() {
return super.getOp();
}
@Override
@Schema(description = "some description", example = "/username", required = true)
public String getPath() {
return super.getPath();
}
@Override
@Schema(description = "some description", , example = "john1991", required = true)
public Object getValue() {
return super.getValue();
}
}
在 PatchOperation.java 中
public class PatchOperation {
/**
* {@link PatchOperator} operation to be performed
*/
@Schema(description = "Operation to be performed", required = true)
@JsonProperty
@NotNull
private PatchOperator op;
@Schema(description = "Path to target where operation will be performed", required = true)
@JsonProperty
@Pattern(regexp = "/([/A-Za-z0-9~])*-*", message = "Invalid path. Please follow regex pattern")
private String path;
@Schema(description = "Value used by operation")
@JsonProperty
private Object value;
public PatchOperation() {
}
}
在 swagger 文档中,UserPatchOp.java 我已经向最终用户展示了如何替换用户名。
在 swagger 上,这个例子附带了 request bogy。
除了用户名通过这个补丁操作,最终用户可以更新描述,然后路径将是/description。
最终用户还可以更新其所属的用户组 '/usergroups' 所以总的来说,我想以同样的方式向 swagger 模式添加更多示例。 但我做不到。因为有一次我只能展示一个例子。
我想在 swagger 页面上向客户端显示以下多个操作。
{
"operationList": [
{
"op": "replace",
"path": "/username",
"value": "john1991"
},
{
"op": "remove",
"path": "/description"
},
{
"op": "add",
"path": "/memberships"
"value":["1224","8907"]
}
]
}
入口方法是
@补丁
@Path("users/{id}")
@Consumes({MediaType.APPLICATION_JSON, APPLICATION_JSON_PATCH_JSON})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = MessageConstants.OK, content = @Content(schema = @Schema(implementation = UserInfo.class))),
@ApiResponse(responseCode = "500", description = MessageConstants.SERVER_ERROR, content = @Content(schema = @Schema(implementation = RestError.class))),
@ApiResponse(responseCode = "400", description = MessageConstants.BAD_REQUEST, content = @Content(schema = @Schema(implementation = RestError.class))),
@ApiResponse(responseCode = "401", description = MessageConstants.UNAUTHORIZED, content = @Content(schema = @Schema(implementation = RestError.class))),
@ApiResponse(responseCode = "404", description = MessageConstants.NOT_FOUND, content = @Content(schema = @Schema(implementation = RestError.class)))})
public Response updatePartialUser(
@Parameter(description = "User Id", required = true) @PathParam("id") String id,
@Parameter(description = "User Update Info", required = true) @Valid PatchOperations<UserPatchOperation> operationList) {
有什么办法,我可以为 getOP、getPath 和 getValue 方法添加多个示例?谢谢。
【问题讨论】:
-
是的,因为应该这样做。如果你想删除用户名,你应该用@DeleteMapping创建一个方法,用@PostMapping添加用户名,用@GetMapping获取它所以最后你将有3个以上的端点/用户名方法。阅读这篇文章,也许它会为你弄清楚一些事情:blog.logrocket.com/…
-
@Szprota21 感谢您的回复。我已经相应地更新了我的问题。我有补丁操作,负责更新我拥有的资源,如用户名、描述和用户组。而且我需要在 swagger 模式中展示多个示例。
标签: java rest swagger swagger-2.0