包含HTTP 405错误的Jersey PathParam [英] Jersey PathParam with HTTP 405 error
问题描述
我正在使用jersey作为我的休息服务器,并且当我尝试将POST请求转发到相对的GET资源时,出现HTTP 405错误.
I'm using jersey for my rest server, and I got a HTTP 405 error, when I try to forward POST request to relative GET resource.
@Path("/")
public class MyResource {
@POST
@Path("/{method}")
@Produces(MediaType.APPLICATION_JSON)
public String postRequest(@PathParam("method") String method, @Context UriInfo uriInfo, String body) throws IOException {
JsonParser parser = new JsonParser();
JsonObject root = parser.parse(body).getAsJsonObject();
JsonObject params = root;
if (root.has("method")) {
method = root.get("method").getAsString();
params = root.getAsJsonObject("params");
}
UriBuilder forwardUri = uriInfo.getBaseUriBuilder().path(method);
for (Map.Entry<String, JsonElement> kv : params.entrySet()) {
forwardUri.queryParam(kv.getKey(), kv.getValue().getAsString());
}
return new SimpleHttpClient().get(forwardUri.toString());
}
@GET
@Path("/mytest")
@Produces(MediaType.APPLICATION_JSON)
public String getTest(@QueryParam("name") String name) {
return name;
}
}
curl -X POST -d {"method":"mytest","params":{"name":"jack"}} localhost/anythingbutmytest
curl -X POST -d {"method":"mytest","params":{"name":"jack"}} localhost/anythingbutmytest
curl -X GET localhost/mytest?name = jack
curl -X GET localhost/mytest?name=jack
这两个上面的卷曲效果很好.但是当我尝试这样请求时,出现405错误:
curl -X POST -d {"method":"mytest","params":{"name":"jack"}}本地主机/mytest
curl -X POST -d {"method":"mytest","params":{"name":"jack"}} localhost/mytest
javax.ws.rs.NotAllowedException: HTTP 405 Method Not Allowed
at org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.getMethodRouter(MethodSelectingRouter.java:466)
at org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.access$000(MethodSelectingRouter.java:94)
......
我该怎么办?
-------------------------------------更新--------- ----------------------------
-------------------------------------Update-------------------------------------
curl -X POST -d {"method":"mytest","params":{"name":"jack"}}本地主机/mytest
curl -X POST -d {"method":"mytest","params":{"name":"jack"}} localhost/mytest
当我添加如下所示的post方法时,此卷曲效果很好. 但是我将为每个这样的GET方法编写一个相同的POST方法,还有其他解决方案吗?
This curl work fine, when I add a post method like below. But I will write a same POST method for every GET Method like that, is there any other solution?
@POST
@Path("/mytest")
@Produces(MediaType.APPLICATION_JSON)
public String postMyTest(@Context UriInfo uriInfo, String body) throws Exception {
return postRequest(uriInfo.getPath(), uriInfo, body);
}
@POST
@Path("/mytest")
@Produces(MediaType.APPLICATION_JSON)
public String postMyTest(@Context UriInfo uriInfo, String body) throws Exception {
return postRequest(uriInfo.getPath(), uriInfo, body);
}
此外,还有其他方法可以将POST请求重新路由到同一类中的方法,而无需构建新的HTTP请求吗?
Besides, is there any other way to re-route POST request to a method in the same class without building a new HTTP request?
推荐答案
您应该使
@POST
@Path("/mytest")
而不是"getTest"方法.原因如下.
and not "getTest" method.Reason is below.
命令
curl -X POST -d {"method":"mytest","params":{"name":"jack"}} localhost/anythingbutmytest
由于以下原因而接受
@Path("/{method}") .
但是
curl -X POST -d {"method":"mytest","params":{"name":"jack"}} localhost/mytest
由于以下原因而不会接受
will not accept because of
@GET
@Path("/mytest")
POST与GET不匹配.
POST does not match GET.
这篇关于包含HTTP 405错误的Jersey PathParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!