【问题标题】:How to fix ' "Missing URI template variable 'uuid' for method parameter of type UUID"'?如何修复“UUID 类型的方法参数缺少 URI 模板变量 'uuid'”?
【发布时间】:2019-05-29 23:59:44
【问题描述】:

我已在 spring-boot 实现的 Web 应用程序的控制器中将路径变量从 String 更新为 UUID。我在前端使用 swagger。我在更改后的响应中收到“缺少 UUID 类型的方法参数的 URI 模板变量 'uuid'”异常。

我已在 spring-boot 实现的 Web 应用程序的控制器中将路径变量从 String 更新为 UUID。我在前端使用 swagger。在数据库方面,我使用的是 mongodb。我正在将此 uuid 转换为字符串以使用为 mongodb 实现的现有 find 方法。我在回复中收到此异常。同样的事情正在另一个项目中工作,找不到为什么它在这里不起作用。

@Path("/uuid")
@RequestMapping(value = "/uuid", method = { RequestMethod.GET })
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Retrieves result based on Unique Identifier", notes = "Returns result matching with the Unique Identifier", 
    position = 1, response = Response.class, httpMethod = "GET")
@ApiResponses(value = { @ApiResponse(code = HttpServletResponse.SC_BAD_REQUEST, message = "Invalid request."),
        @ApiResponse(code = HttpServletResponse.SC_NOT_FOUND, message = "Record not found."),
        @ApiResponse(code = HttpServletResponse.SC_FORBIDDEN, message = "Not authorized for this operation."),
        @ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "Object state out-of-date."),
        @ApiResponse(code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message = "Server error") })
ResponseEntity<Response> getResultByUuid(@ApiParam(required = true, name = "uuid", value = "uuid") @PathParam("uuid") @PathVariable("uuid") UUID uuid,
        @ApiParam(access = "hidden") HttpServletRequest request)
        throws IOException;
  1. 它应该取而代之的是我的结果。现在它抛出异常。它甚至没有到达控制器,怀疑一些弹簧配置依赖的东西。不知道那是什么?

【问题讨论】:

    标签: java spring-boot spring-mvc


    【解决方案1】:

    路径变量必须在大括号中的请求映射中提及。 在您的代码中,以下行

    @RequestMapping(value = "/uuid", method = { RequestMethod.GET })
    

    可以改成

    @RequestMapping(value = "/{uuid}", method = { RequestMethod.GET })
    

    修复该错误。

    【讨论】:

    • 通过这些更改,我最终得到了“为 HTTP 路径映射的模糊处理程序方法”异常。然后我发现父类具有相同的路径,所以使 @RequestMapping(value = "uuid/{uuid}", method = { RequestMethod.GET }) 为我解决了这个问题。感谢您的提示。
    • 请求映射到 /.../uuid for parent 对我来说看起来很不寻常。如果 API 路径的命名约定基于模型的语义,这意味着您将单个 UUID 公开为资源,并且子路径将映射到该 UUID 的属性 - 您的代码示例看起来不像那样。如果这不是您的意图,我建议您更改父母的映射并为孩子保留/{uuid}。示例:/api/v1/users 用于收集所有用户(父),/api/v1/users/{uuid} 用于特定用户(子)。
    • 由于父类来自框架,在项目内部,我们实际上无法修改它。并且 @RequestMapping 在类和父类组合中必须是唯一的,所以我只是修改了我的类(子类)的请求映射并且它起作用了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2022-09-24
    • 2018-06-19
    • 2022-08-05
    • 1970-01-01
    • 2014-10-26
    • 2020-01-14
    相关资源
    最近更新 更多