3

我想为 swagger maven 插件注释我的请求方法,以便在 swagger 请求参数模式中只显示我实际需要的属性,而不是类的所有属性。例如,如果我有一个带有电子邮件和物理地址的人员类,我只需要指定发送普通邮件的物理地址,反之亦然,但生成的招摇会同时显示两个调用。

例子:

示例人员类:

public class Person {
    private String name;
    private String email;
    private String address;
    /* getters / setters */
}

我需要注释的资源类:

@Api
@RequestScoped
@Path("odpiranjeRacuna")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {
    @EJB
    PersonSB personSB;

    @ApiOperation(value="Send an email")
    @POST
    @Path("email")
    public Response sendEmail(@ApiParam Person person){
        return Response.ok().entity(personSB.sendMail(person.getEmail(), person.getName())).build();
    }

    @ApiOperation(value="Send a regular mail")
    @POST
    @Path("snailmail")
    public Response sendSnailmail(@ApiParam Person person){
        return Response.ok().entity(personSB.sendSnailmail(person.getAddress(), person.getName())).build();
    }
}

(略)产生的招摇:

{
  "swagger" : "2.0",
  "paths":{
    "/email" : {
      "post" : {
        "summary" : "Send an email",
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/Person"
          }
        } ]
      }
    },
    "/snailmail" : {
      "post" : {
        "summary" : "Send a regular mail",
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/Person"
          }
        } ]
      }
    }
  },
  "definitions":{
    "Person" : {
      "type" : "object",
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "email" : {
          "type" : "string"
        },
        "address" : {
          "type" : "string"
        }
      }
    }
  }
}

(缩写)想要的招摇:

{
  "swagger" : "2.0",
  "paths":{
    "/email" : {
      "post" : {
        "summary" : "Send an email",
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/PersonWithEmail"
          }
        } ]
      }
    },
    "/snailmail" : {
      "post" : {
        "summary" : "Send a regular mail",
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/PersonWithAddress"
          }
        } ]
      }
    }
  },
  "definitions":{
    "PersonWithEmail" : {
      "type" : "object",
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "email" : {
          "type" : "string"
        }
      }
    },
    "PersonWithAddress" : {
      "type" : "object",
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "address" : {
          "type" : "string"
        }
      }
    }
  }
}

区别在于请求模式引用和模式。

4

0 回答 0