我有一个简单的 REST 客户端,具有 GET POST 和 DELETE 方法。
奇怪的是,只有 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 客户端进行请求,但它们都返回 404,我现在绝望了。
希望您能给我指导。
最佳答案
这看起来可能是 Inheritance with JAX-RS 的重复项。您是否尝试过复制子类中的所有注释,或者不复制,意味着根本不在实现类中使用@PathParam?
关于Java Rest @GET 可以工作,但 @DELETE 和 @POST 路径不会被命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37533150/