Not able to invoke an @DELETE web service in REST/JERSEY(无法在 REST/JERSEY 中调用 @DELETE Web 服务)
问题描述
我正在使用 Jersey 框架(JAX-RS 实现)来构建 RESTful Web 服务.
I am using Jersey Framework (JAX-RS implementation) for building RESTful Web Services.
我无法使用@DELETE REST 方法,因为当我尝试调用它时它会抛出异常.以下@DELETE 方法用于删除员工:
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?
推荐答案
对我来说,它有助于从客户端的 delete() 方法中删除输入参数
For me it helped to remove input parameter from delete() method on client
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).delete(ClientResponse.class);
改为通过@pathParam 发送:
and send it over @pathParam instead:
public String delete(@PathParam("input") String input){...}
这篇关于无法在 REST/JERSEY 中调用 @DELETE Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在 REST/JERSEY 中调用 @DELETE Web 服务
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
