2016-04-11 12 views
6

Ich habe einen einfachen Endpunkt den ich mit dem swagger-maven-plugin bearbeiten möchte. Die resultierende swagger.conf gibt nicht die richtigen "Pfade" für die einzelnen @ApiOperationen wieder. Die Wurzel der API ist "/ api", und ich möchte Endpunkte für GET und PUT zu "/ api/cluster" hinzufügen. Stattdessen lautet die "paths" -Klausel der swagger.json-Ausgabe "/ api".swagger-maven-plugin generiert keine "paths" -Elemente für individuelle Anfragezuordnungen

Hier ist die .java-Quelle, mit @Api (Wert = "/ api") und @RequestMapping (Wert = "/ api") für die Klasse und Einstiegspunkte mit @RequestMapping (Wert = "/ 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); 
    } 
} 

und die pom.xml-Klausel für die Prahlerei-maven-Plugin wie folgt aussieht:

<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> 

der erzeugte swagger.json einen Endpunkt zeigt "/ api", mit dem GET und PUT-Klauseln.

{ 
    "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" 
    } 
    } 
} 

Schließlich mein pom.xml Eintrag für das Prahlerei-Maven-Plugin:

<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> 
+0

Könnten Sie Ihre ' posten 'Abschnitt auch? – Tunaki

+0

Haben Sie '@Path ("/cluster ")' und '@ GET' anstelle von' @RequestMapping (path = "/ cluster", method = RequestMethod.PUT) ' – uniknow

+0

Ich denke, Sie mischen Dinge hier und das kann die Ursache für Ihr Problem sein. Sie verwenden Federn Requestmapping einmal mit "Wert", einmal mit "Pfad" -Parameter. Das Federnetz 4 kennt den Annotationsparameter "path" überhaupt nicht. Also, welche genaue Version von Feder verwendest du? Versuchen Sie, @RequestMapping (Wert = "/ Cluster", Methode = RequestMethod.GET) (d. H. "Wert" anstelle von "Pfad" Annotationsparameter) – TomB

Antwort

3

Vor dem Verfahren versuchen die folgenden

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

Genau richtig ... danke. Und dank @TomB auch für die gleiche Antwort. –

Verwandte Themen