问题
I want to hide the "id" item in the model, how to do this in java?
回答1:
to hide a request field in Swagger API just use the annotation below :)
@ApiModelProperty(hidden = true)
private String id;
And it will be hive
回答2:
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@ApiModelProperty(readOnly = true)
private String id;
Also see: https://github.com/springfox/springfox/issues/2816
回答3:
You can use @Hidden with Swagger Core 2.X
@Hidden -- Hides a resource, an operation or a property
Example from above link : Marks a given resource, class or bean type as hidden, skipping while reading / resolving.
@Path("/user")
@Produces({"application/json", "application/xml"})
public class HiddenAnnotatedUserResourceMethodAndData {
UserData userData = new UserData();
@POST
@Hidden
@Operation(summary = "Create user",
description = "This can only be done by the logged in user.")
@Path("/1")
public Response createUser(
@Parameter(description = "Created user object", required = true) User user) {
userData.addUser(user);
return Response.ok().entity("").build();
}
@POST
@Operation(summary = "Create user",
description = "This can only be done by the logged in user.")
@Path("/2")
public Response createUserWithHiddenBeanProperty(
@Parameter(description = "Created user object", required = true) UserResourceBean user) {
return Response.ok().entity("").build();
}
}
Output of above
openapi: 3.0.1
paths:
/user/2:
post:
summary: Create user
description: This can only be done by the logged in user.
operationId: createUserWithHiddenBeanProperty
requestBody:
description: Created user object
content:
'*/*':
schema:
$ref: '#/components/schemas/UserResourceBean'
required: true
responses:
default:
description: default response
components:
schemas:
UserResourceBean:
type: object
properties:
foo:
type: string
来源:https://stackoverflow.com/questions/53263086/how-to-hide-a-request-field-in-swagger-api