繁体   English   中英

Swagger 编辑器中的多个 @RequestParam 抛出错误

[英]Multiple @RequestParam throwing error in Swagger Editor

我有一个带有 REST 服务的 sprint 启动应用程序,我正在使用 Springfox 库来生成 Swagger json。 在该应用程序中,我有一个使用多部分表单数据的 POST 服务。 我在该 POST 服务中有两个 @RequestParams,其中一个是 MultipartFile,另一个是实际消耗 json 的 String 参数。 但是当我生成 Swagger json 时,只有 Multipart 文件被视为主体参数,但另一个字符串参数被生成为查询参数,从而形成无效的 URL。 并且 POST 服务没有按预期工作

请参考以下服务。

@RequestMapping(value = "/api", method = RequestMethod.POST, consumes= {MediaType.MULTIPART_FORM_DATA_VALUE})
@ResponseBody
@ApiOperation(value = "Publish APIs ", notes = "API helps customer to publish their APIs",
        response = DeferredResult.class, responseContainer="ResponseEntity<APIDeploymentResponse")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Successfully API Published "),
        @ApiResponse(code = 400, message = "Bad request. Validation error occured")})
public DeferredResult<ResponseEntity<APIDeploymentResponse>> publishAPI(
        @ApiParam(name = "file", value = "API Specification file", required = true)
                @RequestParam MultipartFile file,
        @ApiParam(name = "apiDetails", value = "Required API Information", required = true)
                @RequestParam String apiDetails) throws Exception {
    LOG.info("Initiating service to deploy APIs...");

    ApiDetails apidetails = mapper.readValue(apiDetails, ApiDetails.class);

    return serviceRegistryService.apiCreateMode(file, apidetails);

}

下面是我的 POM.xml

   <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jaxrs</artifactId>
        <version>1.5.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
        <scope>compile</scope>
    </dependency>

在 POM.xml 我有以下插件

  <plugin>
            <groupId>com.github.kongchen</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <apiSources>
                  <apiSource>
                        <springmvc>true</springmvc>
                        <locations>com.service.registry.controller</locations>
                        <schemes>http,https</schemes>
                        <host>X.X.X.X:8443</host>
                        <basePath>/service-registry</basePath>
                       <info>
                            <title>Services</title>
                            <version>v1</version>
                            <description>Collection of API to Publish, Update and Remove APIs </description>
                            <termsOfService>
                                http://www.github.com/kongchen/swagger-maven-plugin
                            </termsOfService>
                            <contact>
                                <email>XXX</email>
                            </contact>
                            <license>
                                <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
                                <name>Apache 2.0</name>
                            </license>
                        </info> 
                        <swaggerDirectory>target/generated-sources/swagger-ui</swaggerDirectory>
                    </apiSource>
                </apiSources>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

下面是我的 Swagger 配置文件。

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import springfox.documentation.builders.ApiInfoBuilder;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
 import springfox.documentation.service.ApiInfo;

 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;

 @Configuration
 @EnableSwagger2
 public class SwaggerConfig {
 @Bean
 public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com"))
            .paths(PathSelectors.any())
            //.apiInfo(apiInfo())
            .build();
}
private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title(" API")
            .description("List of Services enables customer to work with 
 their API file")
              .version("2.0")
              .build();
     }
 }

将 apiDetails RequestParam 作为查询的 Swagger Json 的一部分:

 "paths" : {
"/register/api" : {
  "post" : {
    "tags" : [ "register" ],
    "summary" : "Publish APIs ",
    "description" : "API helps customer to publish their API",
    "operationId" : "publishAPI",
    "parameters" : [ {
      "in" : "body",
      "name" : "file",
      "description" : "API Specification file",
      "required" : true,
      "schema" : {
        "$ref" : "#/definitions/MultipartFile"
      }
    }, {
      "name" : "apiDetails",
      "in" : "query",
      "description" : "Required API Information",
      "required" : true,
      "type" : "string"
    } ],
    "responses" : {
      "200" : {
        "description" : "Successfully API Published",
        "schema" : {
          "$ref" : "#/definitions/DeferredResult"
        }
      },
      "400" : {
        "description" : "Bad request. Validation error occured"
      }
    }
  }
},

当我有两个@RequestParams 时,我希望使用 Swagger json。 但是生成的 URL 无效。

https://XXXXXXX.com/YYYYYYY/ZZZZ/register/api?apiDetails= {apiDetails}

在您的控制器方法中,您确实将其指定为@PathVariable

@ApiParam(name = "apiDetails", value = "Required API Information", required = true)
       @PathVariable String apiDetails)

它应该是@RequestParam而不是@PathVariable


或者,如果您打算使用@PathVariable ,它希望您拥有像这样的后期映射:

@PostMapping(value = "/api/" + "{apiDetails}"})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM