java - 如何修复 UUID 类型的方法参数“''缺少 URI 模板变量 ' " 'uuid'?

标签 java spring-boot spring-mvc

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

我已在 spring-boot 实现的 Web 应用程序的 Controller 中将路径变量从 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. 它应该为我获取结果。现在它抛出异常。它甚至没有到达 Controller ,怀疑有一些 spring 配置依赖项。不确定那是什么?

最佳答案

路径变量必须在请求映射中以大括号括起来。 在您的代码中添加以下行

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

可以改为

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

修复该错误。

关于java - 如何修复 UUID 类型的方法参数“''缺少 URI 模板变量 ' " 'uuid'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54021564/

相关文章:

java - 使用JAVA Spring同步rest服务

java - 消息 -"could not read a hi value - you need to populate the table: hibernate_sequence"

java - Spring MVC 设置全局 DateTimeFormat

Java,如何在 apache kafka 中获取主题中的消息数

java - Java 中二维数组创建的维度似乎是倒退的

java - DELETE 操作返回 ERROR : There was an unexpected error (type=Forbidden, status=403)。禁止

mysql - 要使用默认的 BatchConfigurer,上下文必须包含不超过一个数据源,找到 2

java - Spring MVC 和 JSR-303 hibernate 条件验证

java - Spring Boot RESTful 应用程序错误

java - 从数组列表中选取一个随机元素并将其从 Java 列表中删除