0

我正面临一个大摇大摆的代码生成生成存根的问题。我有 2 项服务。首先使用路径 var 和请求参数的两种方法公开 REST api:

    @GetMapping(value = "/start/{pathVar}/operators", params = "login")
    public Operator getOperatorByLogin(
                @ApiParam @PathVariable Long pathVar, 
                @ApiParam(required = true) @RequestParam  String login) {
        return operatorRepository.findDistinctByLogin(login);
    }

    @GetMapping(value = "/start/{pathVar}/operators", params = "ids")
    public List<Operator> getOperatorsByIds(
                @ApiParam @PathVariable Long pathVar, 
                @ApiParam( allowMultiple = true) @RequestParam List<Long> ids) {
        return operatorRepository.findAllByOperatorIdIn(ids);
    }

有 2 个端点具有相同的 URL 但参数不同。Spring-web 框架适用于此。接下来,我生成 OpenApi json 并获得 2 条路径:

"/start/{pathVar}/operators{?ids}": ...
"/start/{pathVar}/operators{?login}": ...

然后,我尝试使用 swagger-codegen-maven-plugin 存根为该端点生成,然后我遇到了问题。

线程“main”java.lang.IllegalArgumentException 中的异常:Map 没有 '?login' 的值

这种形式的 URL 在生成的类中是硬编码的。

(...)
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("pathVar", pathVar);
String path = UriComponentsBuilder.fromPath(
"/start/{pathVar}/opeartors{?login}").buildAndExpand(uriVariables).toUriString();
(...)

由于 uriVariables 中缺少登录映射键值而引发异常。

4

1 回答 1

1

您应该小心您的定义文件(.json 或 .yml)为您的参数定义正确的类型,因为有两种参数:

  1. 路径参数(GET /users/{id})
  2. 查询参数(GET /user/findByLogin?name=myUserLogin)

这两个在 OpenAPI 中有两个不同的声明

1) 路径参数

paths:
  /users/{id}:
    get:
      parameters:
        - in: path
          name: id   # Note the name is the same as in the path
          required: true
          schema:
            type: integer
            minimum: 1
          description: The user ID

2) 查询参数

parameters:
    - in: query
      name: myUserLogin
      schema:
        type: integer
        description: The number of items to skip before starting to collect the result set

更详细的检查官方文档

于 2020-01-09T13:38:04.567 回答