Java Rest @GET 有效但@DELETE 和@POST 路径没有被命中
Java Rest @GET works but @DELETE and @POST paths doesn't get hit
我有一个带有 GET POST 和 DELETE 方法的简单 REST 客户端。
奇怪的是只有 GET 方法有效,POST 和 DELETE 都没有命中,响应当然是“404 Not Found”。
这是我的 REST 服务和客户端:
接口:
public interface MyInterface {
@GET
@Path("/content")
@Produces(MediaType.APPLICATION_JSON)
Response getAirports();
@DELETE
@Path("/content/{id}")
@Produces(MediaType.APPLICATION_JSON)
Response deleteAirport(@PathParam("id") String id);
}
实施:
@Path("/source")
public class SourceService extends AbstractService implements MyInterface {
@Override
public Response getContent() {
DBCollection collection = getDBCollection("content");
DBCursor cursor = collection.find();
String serialize = JSON.serialize(cursor);
return Response.status(Response.Status.OK).entity(serialize).build();
}
@Override
public Response deleteContent(@PathParam("id") Integer id) {
DBCollection collection = getDBCollection("content");
BasicDBObject query = new BasicDBObject();
query.append("id", id);
collection.remove(query);
return Response.status(Response.Status.OK).build();
}
}
客户:
// This is working
public void getContent() {
WebTarget path = collect.path("/content");
Response response = path.request().get();
LOGGER.info("collect.ping: " + response.readEntity(String.class) + "\n");
}
// This is not working
public void deleteContent(Integer id) {
WebTarget path = collect.path("/content/"+id);
Response response = path.request(MediaType.APPLICATION_JSON).delete();
System.out.println("object deleted:"+response);
}
我试过用 jersey 或 apache 客户端请求,但他们都是 return 404,我现在很绝望。
希望大家指点一下。
如果您真的可以调试您的客户端并且您确实能够 "Step through" 客户端代码?
如果您在服务器代码中放置了一个断点,而您实际上从未 "break" 断点?那么问题在于您公开 Web 服务的方式以及您随后尝试使用它的方式。
尝试更改服务器期望的参数类型和您从客户端传递的类型。
如果您可以在服务器和客户端上将其更改为更简单的类型..即..整数..然后您实际上可以在客户端和服务器中捕获断点,那么您就知道问题出在您的类型。
希望你能明白我在说什么?您确实需要简化参数 and/or 先尝试不带参数。
当你得到一些更简单的工作时,你可以将它扩展到其他东西。
尝试将其更改为字符串...例如 "airport" 此外,您在客户端中传递的参数如下:
public void deleteAirport(String iata) {
但是您没有在客户端代码中使用 "iata"...
这看起来可能与 Inheritance with JAX-RS 重复。您是否尝试过复制 subclass 或 none 中的所有注释,意味着根本不在实现 class 中使用 @PathParam?
我有一个带有 GET POST 和 DELETE 方法的简单 REST 客户端。
奇怪的是只有 GET 方法有效,POST 和 DELETE 都没有命中,响应当然是“404 Not Found”。
这是我的 REST 服务和客户端:
接口:
public interface MyInterface {
@GET
@Path("/content")
@Produces(MediaType.APPLICATION_JSON)
Response getAirports();
@DELETE
@Path("/content/{id}")
@Produces(MediaType.APPLICATION_JSON)
Response deleteAirport(@PathParam("id") String id);
}
实施:
@Path("/source")
public class SourceService extends AbstractService implements MyInterface {
@Override
public Response getContent() {
DBCollection collection = getDBCollection("content");
DBCursor cursor = collection.find();
String serialize = JSON.serialize(cursor);
return Response.status(Response.Status.OK).entity(serialize).build();
}
@Override
public Response deleteContent(@PathParam("id") Integer id) {
DBCollection collection = getDBCollection("content");
BasicDBObject query = new BasicDBObject();
query.append("id", id);
collection.remove(query);
return Response.status(Response.Status.OK).build();
}
}
客户:
// This is working
public void getContent() {
WebTarget path = collect.path("/content");
Response response = path.request().get();
LOGGER.info("collect.ping: " + response.readEntity(String.class) + "\n");
}
// This is not working
public void deleteContent(Integer id) {
WebTarget path = collect.path("/content/"+id);
Response response = path.request(MediaType.APPLICATION_JSON).delete();
System.out.println("object deleted:"+response);
}
我试过用 jersey 或 apache 客户端请求,但他们都是 return 404,我现在很绝望。
希望大家指点一下。
如果您真的可以调试您的客户端并且您确实能够 "Step through" 客户端代码?
如果您在服务器代码中放置了一个断点,而您实际上从未 "break" 断点?那么问题在于您公开 Web 服务的方式以及您随后尝试使用它的方式。
尝试更改服务器期望的参数类型和您从客户端传递的类型。
如果您可以在服务器和客户端上将其更改为更简单的类型..即..整数..然后您实际上可以在客户端和服务器中捕获断点,那么您就知道问题出在您的类型。
希望你能明白我在说什么?您确实需要简化参数 and/or 先尝试不带参数。
当你得到一些更简单的工作时,你可以将它扩展到其他东西。
尝试将其更改为字符串...例如 "airport" 此外,您在客户端中传递的参数如下:
public void deleteAirport(String iata) {
但是您没有在客户端代码中使用 "iata"...
这看起来可能与 Inheritance with JAX-RS 重复。您是否尝试过复制 subclass 或 none 中的所有注释,意味着根本不在实现 class 中使用 @PathParam?