gpt4 book ai didi

java - 发送 404 而不是 405,后者是空的 WebApplicationException

转载 作者:可可西里 更新时间:2023-11-01 16:37:47 25 4
gpt4 key购买 nike

我有 dropwizard.io 应用程序,GET 请求有问题,我的资源如下所示:

@Path("/foobar")
public class FooBarResource
(...)
@GET
@Path("/{id}")
@UnitOfWork
public Response getFooBar( @PathParam("id") Long id){
return Response.status(200).entity(FooBarService.get(id)).build();
}

@DELETE
@Path("/{id}")
@UnitOfWork
public Response getFooBar( @PathParam("id") Long id){
return Response.status(200).entity(FooBarService.delete(id)).build();
}

@PUT
@Path("/{id}")
@UnitOfWork
public Response getFooBar( @PathParam("id") Long id, FooBar fooBar){
return Response.status(204).entity(FooBarService.update(id, fooBar)).build();
}
}

当我发送 GET localhost:port/appPath/foobar/ 时,我得到的是 405 而不是 404。我怎样才能得到 404?当我调试我的应用程序时,我只有一个 javax.ws.rs.WebApplicationException,但它是空的。

最佳答案

您尝试 GET localhost:port/appPath/foobar/并得到 405(方法不允许)。原因是这个资源(=foobar)存在,所以404(找不到资源)是错误的。如果您在没有路径注释的情况下创建 getFooBar() -> getFooBar2() 的副本,您应该得到 200。

如果你愿意,你可以创建一个容器过滤器,如果没有准确找到路径,它会抛出异常。然后可以将该异常映射到 404。

关于java - 发送 404 而不是 405,后者是空的 WebApplicationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26274460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com