Chi*_*lax 2 java rest web-services jersey
我正在使用Jersey Framework(JAX-RS实现)来构建RESTful Web服务.
我无法使用@DELETE REST方法,因为它在我尝试调用它时抛出异常.以下@DELETE方法用于删除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();
Run Code Online (Sandbox Code Playgroud)
}
我正在使用以下代码块来调用服务:
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);
Run Code Online (Sandbox Code Playgroud)
当我运行我的客户端时,它会抛出以下异常:
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)
Run Code Online (Sandbox Code Playgroud)
如果有人可以指导我如何解决它会很棒吗?
ini*_*goD 12
这是HTTPUrlConnection类实现中的java错误:
http://bugs.sun.com/view_bug.do?bug_id=7157360
它应该在java 8中解决.....
同时,实际上,要将json发送到REST服务,您必须发送路径参数,如@Micer所说,或者让您自己的HttpUrlConnection将请求方法设置为POST并使用"X-HTTP_method-Override"覆盖它说你想要DELETE做这样的事情:
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());
}
Run Code Online (Sandbox Code Playgroud)
来源:https://groups.google.com/a/openflowhub.org/forum/#!msg/floodlight-dev/imm_8drWOwU/Uu4h_u3H6HcJ
| 归档时间: |
|
| 查看次数: |
35992 次 |
| 最近记录: |