Java Rest @GET 可以工作,但 @DELETE 和 @POST 路径不会被命中

标签 java web-services rest client response

我有一个简单的 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/

相关文章:

java - J2ME 网络服务

rest - 为什么我们在 Restful API 中使用不同的动词?

java - 使用反射启动 JavaFX 项目

java - Spring Batch 的 ItemWriter 是单例类吗?

javax.validation 如何针对不同的语言环境?

postgresql - 从 PostgreSQL 过程/函数调用 RESTful Web 服务

java - 我试图在 TextToSpeech 运行时更改 textView 的背景,这可能吗?

java - 如何在 Servlet 中调用 java Rest WebService

rest - 为 REST 资源提供极其激进的 HTTP 缓存的建议

xml - 海康威视日志搜索 REST API(POST 方法)给出无效的 XML 格式