Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

java - Not able to invoke an @DELETE web service in REST/JERSEY

I am using Jersey Framework (JAX-RS implementation) for building RESTful Web Services.

I'm not able to use the @DELETE REST method, since its throwing an exception when I try to invoke it.The following @DELETE method is used to delete an Employee:

@Path("/employees")
public class EmpResource {

@DELETE
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteEmployee(JAXBElement<String> r) throws ClassNotFoundException, SQLException {

    String name = r.getValue();
    String response = DAOaccess.deleteEmp(name);
    return Response.noContent().build();    

}

And I'm using the following block of code to invoke the service:

Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/RestApp/sample/employees");
String input = "{"name":"John"}";
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).delete(ClientResponse.class,input);

When I run my client, it throws the following exception:

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: HTTP method DELETE doesn't support output
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.delete(WebResource.java:599)

It would be great if someone could guide me on how to resolve it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a java's bug in the implementation of the HTTPUrlConnection class:

http://bugs.sun.com/view_bug.do?bug_id=7157360

It should be solved in java 8.....

Meanwhile, in the real word, to send a json to a REST service you must send path parameter(s) like @Micer says or make your own HttpUrlConnection setting the requestmethod to POST and overriding it with the "X-HTTP_method-Override" to say that you want a DELETE doing something like this:

  public static void remove(String dflowname) throws IOException, ParseException {
    String jsonResponse = "";

    URL url = new URL(deleteuri);
    HttpURLConnection connection = null;
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    // We have to override the post method so we can send data
    connection.setRequestProperty("X-HTTP-Method-Override", "DELETE");
    connection.setDoOutput(true);

    // Send request
    OutputStreamWriter wr = new OutputStreamWriter(
      connection.getOutputStream());
    wr.write(dflowname);
    wr.flush();

    // Get Response
    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
      jsonResponse = jsonResponse.concat(line);
    }
    wr.close();
    rd.close();

    JSONParser parser = new JSONParser();
    JSONObject obj = (JSONObject) parser.parse(jsonResponse);
    System.out.println(obj.get("status").toString());
  }

source: https://groups.google.com/a/openflowhub.org/forum/#!msg/floodlight-dev/imm_8drWOwU/Uu4h_u3H6HcJ


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...