代码之家  ›  专栏  ›  技术社区  ›  Carsten

在Swagger API中声明为不需要的QueryParam

  •  0
  • Carsten  · 技术社区  · 6 年前

    import javax.ws.rs.DefaultValue;
    import javax.ws.rs.HeaderParam;
    import javax.ws.rs.POST;
    import javax.ws.rs.QueryParam;
    import org.hibernate.validator.constraints.NotEmpty;
    [...]
    
    @POST
    @Timed
    public Prediction predict(
            @QueryParam("content") @NotEmpty String content,
            @HeaderParam("outputProbability") @DefaultValue("false") Boolean outputProbability) {
        return outputProbability ? getPredictionWithProb(content) : getPrediction(content);
    }
    

    pom.xml swagger-maven-plugin 这样地:

            <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>${swagger-maven-plugin-version}</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <springmvc>false</springmvc>
                            <schemes>
                                <scheme>http</scheme>
                            </schemes>
                            <locations>[...]</locations>
                            <info>[...]</info>
                            <swaggerDirectory>src/main/resources/swagger</swaggerDirectory>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    当我跑的时候 mvn compile ,它创建 swagger.json 包含以下项的文件:

    "paths" : {
    "/predict" : {
      "post" : {
        "operationId" : "predict",
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "name" : "content",
          "in" : "query",
          "required" : false,
          "type" : "string"
        }, {
          "name" : "outputProbability",
          "in" : "header",
          "required" : false,
          "type" : "boolean",
          "default" : false
        } ],
    [...]
    

    这一切都很好,除了一行 content 参数定义:

          "required" : false,
    

    内容 内容

    this answer ,似乎我可以通过使用Swagger显式地声明参数是必需的 @ApiParam annotation . 但是,我不希望仅仅为了swaggerapi定义的目的而引入额外的代码和依赖项。

    swagger maven插件

    Swagger插件是否无法识别 @org.hibernate.validator.constraints.NotEmpty @OpenAPI 参数声明Swagger插件所需参数的唯一方法是什么?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Carsten    6 年前

    我找到的唯一有效的解决办法就是使用 @ApiParam 注释如下:

    import io.swagger.annotations.ApiParam;
    [...]
    
    
    @POST
    @Timed
    public Prediction predict(
            @QueryParam("content") @NotEmpty @ApiParam(required = true) String content,
            @HeaderParam("outputProbability") @DefaultValue("false") Boolean outputProbability) {
    

    当然,这需要一个额外的招摇依赖(在 pom.xml ):

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>