Quarkus 返回 404 而不是 400

问题描述 投票:0回答:2
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/greeting/{name}")
public Uni<String> greeting(String name, @QueryParam("limit") int limit) {
     return service.greeting(name.toString());
}

localhost:8080/greeting/someName?limit=454——按预期返回 200 localhost:8080/greeting/someName?limit=dfg——这个返回 404 而不是 400

在 Quarkus 应用程序中,端点返回错误的错误代码(404 而不是 400)

但是在 Spring boot 非反应性应用程序中,这工作正常(返回 400)

http-status-code-404 quarkus http-status-code-400 quarkus-reactive
2个回答
0
投票

我认为 quarkus 选择将此视为错误

404
因为它期待具有以下签名的方法:

public Uni<String> greeting(String name, @QueryParam("limit") String limit) {
   ...
}

它找不到一个。

解决用例的一种方法是将限制读取为字符串,然后将其转换为整数:


    // Convert a NumberFormatException error into a 400 status code response
    @ServerExceptionMapper
    public RestResponse<String> mapException(NumberFormatException x) {
        return RestResponse.status(Response.Status.BAD_REQUEST, "Unknown limit: " + x.getMessage());
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/greeting/{name}")
    public Uni<String> greeting(String name, @QueryParam("limit") String limit) {
        // Throws NumberFormatException if the limit is not a number
        int limitAsInt = Integer.parseInt( limit );
        return service.greeting(name.toString());
    }

0
投票

查询参数转换错误被威胁为 404(而不是预期的 400)作为规范(https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest3x/jaxrs-resources.html# d0e2052).

© www.soinside.com 2019 - 2024. All rights reserved.