【发布时间】:2016-05-30 20:20:11
【问题描述】:
我有一个带有 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 客户端请求,但它们都返回 404,我现在已经绝望了。
希望你能给我一个方向。
【问题讨论】:
-
尽量减少你的问题。使实现立即返回 OK 响应。
-
@Chill 刚刚尝试删除 PathParam,它被击中并返回 OK。那个怎么样?我是否以错误的方式发送路径参数?
-
尝试将其更改为字符串...例如“airport” 此外,您正在客户端中传递一个参数,如下所示: public void deleteAirport(String iata) { 但你不使用客户端代码中的“iata”...
-
@Chill 再次尝试了它没有击中......它只有在没有 PathParam 存在时才会击中。我应该以不同的方式处理 PathParam 吗?
-
请评论我的回答。
标签: java web-services rest client response