以下代码是应用程序中控制器的一部分。
@GET
@Path("/limit/{limit}/offset/{offset}")
@Produces(MediaType.APPLICATION_JSON)
public Response paginatedAccounts(
@Parameter(
description = "Number of records to be returned.",
required = true,
schema = @Schema(type = SchemaType.INTEGER))
@PathParam("limit") int limit,
@Parameter(
description = "The starting number of record, zero based.",
required = true,
schema = @Schema(type = SchemaType.INTEGER))
@PathParam("offset") int offset)
{
return Response
.ok(this.accountService.getPaginatedAccounts(limit, offset))
.build();
}它返回一个分页的帐户列表。
当用户调用为“限制”或“偏移”提供错误类型的API时,即:
http://[url]/[entity]/limit/zzz/offset/0
她收到"404 - Not“
如何验证参数“限制”和“偏移”,以便当用户提供错误类型(string for int)时,她会收到:
"400 -糟糕的请求“
应该是这样吗?
发布于 2021-04-06 17:23:48
这是由设计( JAX-RS规范)。
https://docs.oracle.com/cd/E19798-01/821-1841/6nmq2cp1v/index.html明确地提到了它:
如果URI路径模板变量不能转换为指定的类型,JAX运行时将向客户端返回一个HTTP 400 (“坏请求”)错误。如果不能将@PathParam注释转换为指定类型,JAX-RS运行时将向客户端返回一个HTTP 404 (“未找到”)错误。
https://stackoverflow.com/questions/66970838
复制相似问题