java - swagger-maven-plugin 不会为单个请求映射生成 "paths"元素

标签 java spring maven swagger swagger-maven-plugin

我有一个简单的端点,我想用 swagger-maven-plugin 处理。生成的 swagger.conf 未反射(reflect)单个 @ApiOperations 的正确“路径:”。 api 的根是“/api”,我想将 GET 和 PUT 的端点添加到“/api/cluster”。相反,swagger.json 输出的“路径:”子句是“/api”。

这是 .java 源代码,类上有 @Api(value="/api") 和 @RequestMapping(value="/api"),入口点有 @RequestMapping(value = "/cluster") :

ClusterManagerController.java:

    package com.vmturbo.clustermgr;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;

/**
 * REST endpoint for ClusterMgr, exposing APIs for component status, component configuration, node configuration.
 **/
@Component
@RestController
@Api(value = "/api", description = "Methods for managing the Ops Manager Cluster")
@RequestMapping(value="/api", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE})
public class ClusterMgrController {

    @Autowired
    private ClusterMgrService clusterMgrService;

    /**
     * Get a dump of the current Cluster Configuration
     *
     * @return a {@link com.vmturbo.clustermgr.ClusterMgrService.ClusterConfiguration} object containing known components,
     * components associated with each node, and property key/value maps for each component.
     */
    @ApiOperation(value = "Get a dump of the current cluster configuration")
    @RequestMapping(path = "/cluster",
            method = RequestMethod.GET)
    @ResponseBody
    public ClusterMgrService.ClusterConfiguration getClusterConfiguration() {
        return clusterMgrService.getClusterConfiguration();
    }

    /**
     * Replace the current Cluster Configuration with a new one.
     *
     * @return the new Cluster configuration, read back from the key/value store.
     */
    @ApiOperation(value = "Replace the current Cluster Configuration with a new one.")
    @RequestMapping(path = "/cluster",
            method = RequestMethod.PUT)
    @ResponseBody
    public ClusterMgrService.ClusterConfiguration setClusterConfiguration(
            @RequestBody ClusterMgrService.ClusterConfiguration newConfiguration) {
        return clusterMgrService.setClusterConfiguration(newConfiguration);
    }
}

swagger-maven-plugin 的 pom.xml 子句如下所示:

    <plugin>
        <groupId>com.github.kongchen</groupId>
        <artifactId>swagger-maven-plugin</artifactId>
        <version>3.1.1</version> <!-- TODO: move swagger version to top-level pom -->
        <configuration>
            <apiSources>
                <apiSource>
                    <springmvc>true</springmvc>
                    <schemes>http,https</schemes>
                    <basePath>/</basePath>
                    <locations>com.vmturbo.clustermgr.ClusterMgrController</locations>
                    <info>
                        <title>ClusterMgr REST API</title>
                        <version>v1</version>
                        <description>The API for configuration and control of a VMTurbo XL Ops Manager Cluster</description>
                    </info>
                    <swaggerDirectory>${swagger.conf.directory}</swaggerDirectory>
                </apiSource>
            </apiSources>
        </configuration>
        <executions>
            <execution>
                <phase>prepare-package</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

生成的 swagger.json 显示一个端点“/api”,带有 GET 和 PUT 子句。

{
  "swagger" : "2.0",
  "info" : {
    "description" : "The API for configuration and control of a VMTurbo XL Ops Manager Cluster",
    "version" : "v1",
    "title" : "ClusterMgr REST API"
  },
  "basePath" : "/",
  "tags" : [ {
    "name" : "api",
    "description" : "Methods for managing the Ops Manager Cluster"
  } ],
  "schemes" : [ "http", "https" ],
  "paths" : {
    "/api" : {
      "get" : {
        "tags" : [ "api" ],
        "summary" : "Get a dump of the current cluster configuration",
        "description" : "",
        "operationId" : "getClusterConfiguration",
        "produces" : [ "application/json", "text/plain" ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/ClusterConfiguration"
            }
          }
        }
      },
      "put" : {
        "tags" : [ "api" ],
        "summary" : "Replace the current Cluster Configuration with a new one.",
        "description" : "",
        "operationId" : "setClusterConfiguration",
        "produces" : [ "application/json", "text/plain" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/ClusterConfiguration"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/ClusterConfiguration"
            }
          }
        }
      }
    }
  },
  "definitions" : {
    "ClusterConfiguration" : {
      "type" : "object",
      "properties" : {
        "nodes" : {
          "type" : "object",
          "readOnly" : true,
          "additionalProperties" : {
            "$ref" : "#/definitions/ComponentPropertiesMap"
          }
        }
      }
    },
    "ComponentPropertiesMap" : {
      "type" : "object"
    }
  }
}

最后,我的 swagger-maven-plugin 的 pom.xml 条目:

    <plugin>
        <groupId>com.github.kongchen</groupId>
        <artifactId>swagger-maven-plugin</artifactId>
        <version>3.1.1</version> <!-- TODO: move swagger version to top-level pom -->
        <configuration>
            <apiSources>
                <apiSource>
                    <springmvc>true</springmvc>
                    <schemes>http,https</schemes>
                    <basePath>/</basePath>
                    <locations>com.vmturbo.clustermgr.ClusterMgrController</locations>
                    <info>
                        <title>ClusterMgr REST API</title>
                        <version>v1</version>
                        <description>The API for configuration and control of a VMTurbo XL Ops Manager Cluster</description>
                    </info>
                    <swaggerDirectory>${swagger.conf.directory}</swaggerDirectory>
                </apiSource>
            </apiSources>
        </configuration>
        <executions>
            <execution>
                <phase>prepare-package</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

最佳答案

在尝试使用以下方法之前

 @RequestMapping(value = "/cluster", method = RequestMethod.GET)

关于java - swagger-maven-plugin 不会为单个请求映射生成 "paths"元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36556534/

相关文章:

java - junit测试与真实源码混合情况下的POM文件

java - 使用 CommonsMultipartResolver (Spring 3) 上传文件时是否可以识别文件格式

java - 保存 Runnable 实现的函数

java - Servlet POST 数据根据调用者进行裁剪

java - @PostContruct 在服务器启动期间给出 NullPointerException

java - 如何在单个服务器上将 Hibernate 与多个数据库一起使用

java - 错误: Could not find or load main class Main in CMD

eclipse - eclipse新项目中的maven集成 checkout 但无法导航

java - Apache Storm 中的自定义序列化

spring - Togglz 与 Spring @Configuration bean