【发布时间】:2019-12-20 10:57:56
【问题描述】:
我正在查看一个提供 REST API 来访问数据库的 Java 应用程序。除了 JAX-RS,它还使用 Swagger 生成文档和测试网站。在其中一条路线上,我意识到 Swagger 给出的结果示例非常长(超过 140000 行):
这条路线是用以下代码定义的:
@GET
@Path("/getUsers")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Operation(
summary = "Get available users",
responses = {
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = OauthClientDetailsEntity.class)), description = "Get users list"),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(implementation = ErrorResponse.class)), description = "Error: Unauthorized"),
@ApiResponse(responseCode = "500", description = "Error: Internal Server Error")
}
)
public Response getUsers() {
// ....
}
“麻烦制造者”是idCliente字段,在实体类中是这样定义的:
@JoinColumn(name = "idCliente", referencedColumnName = "id")
@ManyToOne
private Cliente idCliente;
表Cliente(英文的customer)与DB中的很多表相关,所以我认为问题在于Swagger正在遍历所有这些表,所以示例结果很长。该类有40多个字段,如下所示:
@Entity
@Table(name = "cliente")
@XmlRootElement
public class Cliente implements Serializable {
@OneToMany(mappedBy = "idCliente")
private List<FacturaCliente> facturaClienteList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idCliente")
private List<OauthClientDetailsEntity> oauthClientDetailsEntityList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "cliente")
private List<OauthAccessTokenEntity> oauthAccessTokenEntityList;
@Column(name = "codigoPlantillaFacturacion")
private String codigoPlantillaFacturacion;
// etc...
}
我是 Swagger 的新手,所以我不确定如何处理这个问题。有什么办法可以让 Swagger 不遍历所有相关表?或者,还有什么其他方法更有效?
提前致谢,
【问题讨论】:
-
User 类是什么样的?