英文:
Is it possible to do path-wise documentation of API endpoints with Swagger Java annotations?
问题
我有一个名为“Person”的类,根据端点的不同,可能需要或不需要其“firstName”。在Java中,是否有一种使用Swagger OpenAPI文档标签来实现这一点的方法?
如果可能的话,我想它可能看起来有点像这样:
public class Person {
    @Path("/endpoint1", required = true)
    @Path("/endpoint2", required = false)
    private String firstName;
}
英文:
I have, say, a class Person which, depending on the endpoint, would have its firstName either required or not. Is there a way of doing this with Swagger OpenAPI documentation tags in Java?
I'm imagining it could look a bit like this if it were/is possible:
public class Person {
    @Path("/endpoint1", required = true)
    @Path("/endpoint2", required = false)
    private String firstName;
}
答案1
得分: 1
你需要两个类 - 一个其中firstName属性是必需的,另一个其中firstName是可选的。为了减少代码重复,你可以定义一个基类,其中包含除firstName之外的所有属性,并从这个基类继承另外两个Person类。
英文:
You'll need two classes - one where the firstName property is required, and another one where it's optional. To reduce code duplication, you can define a base class that contains all properties except for firstName and inherit the other two Person classes from this base class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论