1

我有一个 jaxrs 应用程序,它有两个资源路径,每个路径都产生不同的内容类型。我正在尝试生成一个 openapi 文件(与 swagger-ui 一起使用),但只生成了一种内容类型。

看这个例子:

@Path("test")
public class TestResource {

  @GET
  @Produces({MediaType.APPLICATION_JSON})
  public Response getListJ(){
    return Response.ok().entity("\"foo\" : \"bar\"").build();
  }

  @GET
  @Produces({MediaType.TEXT_HTML})
  public Response getListH(){
      return Response.ok().entity("<html></html>").build();
  }
}

如果我运行 swagger-maven-plugin

.
.
    <dependencies>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-jaxrs2</artifactId>
            <version>2.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <configuration>
                    <outputFileName>test-api</outputFileName>
                    <outputPath>${project.build.directory}</outputPath>
                    <outputFormat>JSONANDYAML</outputFormat>
                    <resourcePackages>
                        <package>my.test.package</package>
                    </resourcePackages>
                    <prettyPrint>true</prettyPrint>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>resolve</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
.
.

它产生

    "/test" : {
      "get" : {
        "operationId" : "getListH_1",
        "responses" : {
          "default" : {
            "description" : "default response",
            "content" : {
              "text/html" : { }
            }
          }
        }
      }
    }

我的问题是它只生成文本/html。当我将两者@Produces合并为一个资源时:

@Path("test")
public class TestResource {

  @GET
  @Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML})
  public Response getList(){
    entity = getEntity(); // implementation not shown
    return Response.ok().entity(entity).build();
  }
}

然后它产生

    "/test" : {
      "get" : {
        "operationId" : "getList",
        "responses" : {
          "default" : {
            "description" : "default response",
            "content" : {
              "application/json" : { },
              "text/html" : { }
            }
          }
        }
      }
    }

难道我做错了什么?或者这是一个错误?

4

0 回答 0