ToHe
ToHe

Reputation: 147

DELETE with Jersey REST webservice client always throws 404

I have implemented a generic REST webservice client. Everything is working correctly except the delete request. It always returns a "404 -> NOT FOUND" error, but when using other tools (firefox poster & curl), I can execute the delete request, so the webservice client is working and running.

The method that is failing:

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);
    }
}

When executing the 'builder.delete(returnClass)' statement a 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)

The webResource is the same as for my other requests, so that's not the problem nor are the variables (baseURL, urlExtension, returnClass & customHeaders) are correct and when I use those values in other test tools, I get a correct response.

Anybody knows why I am always getting this 404 error?

Upvotes: 2

Views: 2141

Answers (1)

Martin Matula
Martin Matula

Reputation: 7989

Please note WebResource is immutable. WebResource.path() does not alter the existing WebResource. It creates a new one. So I am guessing, after the Validation... lines, instead of this:

webResource.path(urlExtension);

you want to do this:

webResource = webResource.path(urlExtension);

Upvotes: 2

Related Questions