2016-04-11 15 views
6

Basit bir son noktaya, swagger-maven-plugin ile işlemek istiyorum. Ortaya çıkan swagger.conf, @ApiOperations bireysel için doğru "yolları:" yansıtmıyor. Api'nin kökü "/ api" dir ve GET ve PUT için "/ api/cluster" için son noktalar eklemek istiyorum. Bunun yerine, swagger.json çıktısının "yolları:" ifadesi "/ api" dir.swagger-maven-plugin, bireysel istek eşleştirmeleri için "yollar" öğeleri oluşturmaz

Sınıftaki @Api (value = "/ api") ve @RequestMapping (value = "/ api") ve @RequestMapping (value = "/ cluster" ile giriş noktaları içeren .java kaynağıdır.):

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); 
    } 
} 

ve gibi çalım-maven-eklentisi için pom.xml şartı arar:

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

oluşturulan swagger.json bir son nokta, "/ aPI" gösterir GET ile ve PUT cümleleri.

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

Son olarak, caka-maven-eklenti için benim pom.xml girdisi:

<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

Eğer ' gönderir misiniz kullanmayı deneyin bölüm de? – Tunaki

+0

@RequestMapping (yol = "/ cluster", method = RequestMethod.PUT) '' yerine '@Path ("/ cluster") 've' @ GET' denediniz mi? – uniknow

+0

Bence buradaki şeyleri karıştırıyorsunuz. probleminizin nedeni olabilir. "Path" parametreleri ile bir kez "value" ile bir kez Requestmapping, yayları kullanıyorsunuz. Spring web 4, "yol" ek açıklama parametresini hiç bilmiyor. Peki baharın tam sürümünü nasıl kullanıyorsunuz? @RequestMapping (value = "/ cluster", yöntem = RequestMethod.GET) ("yol" açıklaması yerine "value" yerine) kullanın. – TomB

cevap

3

yöntemle önce aşağıdaki

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

Tam olarak sağ ... teşekkürler. Ve aynı cevap için @TomB'ye teşekkürler. –