从java客户端(HTTPPost)调用java web服务方法
我想从java客户机执行java web服务中定义的方法。我使用ApacheTomcat7在本地托管web服务。我正在使用HttpPost方法,但我认为我使用了错误的url。以下是web服务的代码:从java客户端(HTTPPost)调用java web服务方法,java,apache,web-services,tomcat7,Java,Apache,Web Services,Tomcat7,我想从java客户机执行java web服务中定义的方法。我使用ApacheTomcat7在本地托管web服务。我正在使用HttpPost方法,但我认为我使用了错误的url。以下是web服务的代码: public class Service{ @POST @Path("/getString") public String getString(){ String str = "Hello"; return str; } } 以下是调
public class Service{
@POST
@Path("/getString")
public String getString(){
String str = "Hello";
return str;
}
}
以下是调用该方法的客户端代码:
public class Client{
public static void main(String[] args) throws Exception{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:8080/WebService/getString");
post.setHeadder("Content-Type", "application/xml");
HttpResponse httpres = client.execute(post);
HttpEntity entity = httpres.getEntity();
System.out.println("HTTP Response:" + EntityUtils.toString(entity));
}
}
客户端的响应是404,未找到请求的资源“WebService/getString”
请告诉我在客户端中应该在哪里提到方法名称?我在url“WebService/getString”中提到了它,还提到了@Path注释。但是为什么它不能在web服务中获得getString()方法呢?请帮忙。。。
提前感谢…可能是因为您的类名为Service而不是WebService?我尝试了,但仍然没有得到响应。我解决了这个问题。我的url错误,与类名无关。