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

问题描述 投票:0回答:1

我已经实现了一个包含此方法的Jax-RS资源(使用Dropwizard):

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,

但是,显然需要content字段。当我调用服务时也会确认:如果未提供content参数,则会引发错误。

this answer,似乎我可以通过使用Swagger @ApiParam annotation明确声明该参数是必需的。但是,我不希望仅为Swagger API定义的目的引入其他代码和依赖项。

这看起来像一个相当小的问题,但它可能表明我的代码甚至swagger-maven-plugin中的错误。我错过了什么吗?

Swagger插件是否无法识别@org.hibernate.validator.constraints.NotEmpty注释?如果没有,Swagger @OpenAPI参数是否是声明Swagger插件所需参数的唯一方法?

java maven swagger openapi
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.