使用Jersey REST Web服务客户端删除始终会抛出404

时间:2011-12-07 15:14:07

标签: web-services jersey client http-status-code-404 http-delete

我实现了一个通用的REST Web服务客户端。除删除请求外,一切正常。它总是返回“404 - > NOT FOUND”错误,但是当使用其他工具(firefox poster& curl)时,我可以执行删除请求,因此webservice客户端正在运行。

失败的方法:

private <T> Object sendDelete(String baseURL, String urlExtension, Class<T> returnClass, CustomHeaders customHeaders) throws WebServiceDeleteRequestException {

    Validate.notNull(baseURL, "The specified Base URL is NULL!");
    Validate.notEmpty(baseURL, "The specified Base URL is empty!");
    Validate.notNull(urlExtension, "The specified URL Extension is NULL!");
    Validate.notEmpty(urlExtension, "The specified URL Extension is Empty!");
    Validate.notNull(returnClass, "The specified Class to return is NULL!");

    WebResource webResource = null;

    try {

        webResource = getRESTClient().resource(baseURL);

    } catch (ServiceClientException serviceClientException) {

        throw new WebServiceDeleteRequestException("Couldn't execute the HTTP DELETE request! The ServiceRESTClient couldn't be created!", serviceClientException);
    }

    webResource.path(urlExtension);

    try {
        if(customHeaders == null) {

            return webResource.delete(returnClass);

        } else {

            WebResource.Builder builder = webResource.getRequestBuilder();

            if(customHeaders != null) {

                for(Entry<String, String> headerValue : customHeaders.getCustomHeaders().entrySet()) {
                    builder.header(headerValue.getKey(), headerValue.getValue());
                }
            }

            builder.accept(MediaType.APPLICATION_XML);

            return builder.delete(returnClass);
        }
    } catch (Exception exception) {

        throw new WebServiceDeleteRequestException("Couldn't execute the HTTP DELETE request!", exception);
    }
}

执行'builder.delete(returnClass)'语句时出现UniformInterfaceException。

com.sun.jersey.api.client.UniformInterfaceException: DELETE http://localhost:8080/db/ returned a response status of 404 Not Found
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:676)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.delete(WebResource.java:583)

webResource与我的其他请求相同,所以这不是问题,也不是变量(baseURL,urlExtension,returnClass和ampHeader)是正确的,当我在其他测试工具中使用这些值时,我得到了一个正确的响应。

有人知道为什么我总是收到这个404错误吗?

1 个答案:

答案 0 :(得分:2)

请注意WebResource是不可变的。 WebResource.path()不会改变现有的WebResource。它创造了一个新的。所以我猜测,在验证...行后,而不是这个:

webResource.path(urlExtension);

你想这样做:

webResource = webResource.path(urlExtension);