jdk1.7调用webService(1.6相同)
一.服务端代码:
package jws.service;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import javax.jws.WebMethod;
@WebService
public class JavaWs {
@WebMethod//可不写
public String doSomething(){
return "Service invoked!";
}
public static void main(String[] args){
String url = "http://localhost:8889/javaWebService/jws.service.JavaWs";
Endpoint.publish(url, new JavaWs());
}
}
二.启动服务后通过wsimport工具创建客户端
1.服务启动后在浏览器里打上
http://localhost:8889/javaWebService/jws.service.JavaWs?wsdl
后会在页面上显示一个xml文件内容,此时表明服务已经开启。
2.在cmd里cd到src文件夹下打上命令:
wsimport –p jws.client –keep http://localhost:8889/javaWebService/jws.service.JavaWs?wsdl
注:
1).当wsimport不识别时,在环境变量里配置一下C:\Program Files\Java\jdk1.7.0_01\bin,该文件夹里有个wsimport.exe可执行文件
2).千万不能开代理,否则也无法生成客户端
3.调用webservice
import jws.client.JavaWs;
import jws.client.JavaWsService;
JavaWs client = new JavaWsService().getJavaWsPort();
System.out.println(client.doSomething());
java调用.net的wcf
.net的server端:
//服务接口
[ServiceContract]
public interface IAthorService
{
[OperationContract]
string Test(string str);
}
//服务实现类
public class AthorService : IAthorService
{
public string Test(string str)
{
return str + "Hellow World!";
}
}
//启动服务
private void BeginService()
{
//step 1
Uri addr = new Uri("http://localhost:8000/wcf/Service");
//step 2
ServiceHost host = new ServiceHost(typeof(AthorService), addr);
try
{
//step 3
//必须要new BasicHttpBinding,而不是WSHttpBinding,否则java端wsimport后无法生成IAthorService和AthorService
//host.AddServiceEndpoint(typeof(IAthorService), new WSHttpBinding(), "AthorService");
host.AddServiceEndpoint(typeof(IAthorService), new BasicHttpBinding(), "AthorService");
//step 4
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
//true表示wcf服务会生成wsdl,否则无法被java调用
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);
//step 5
host.Open();
}
catch (CommunicationException ce)
{
host.Abort();
}
}
java的client端:
1.通过wsimport生成客户端
wsimport -p ws.client -keep http://localhost:8000/wcf/Service?wsdl
2.调用服务方法
import ws.client.*;
public class Run {
public static void main(String[] args){
AthorService client = new AthorService();
String str = client.getBasicHttpBindingIAthorService().test("guantong ");
System.out.println(str);
}
}
.net的client端:
1.客户端添加service服务
http://localhost:8000/wcf/Service?wsdl 也可以
点OK,添加成功
2.客户端调用服务方法
AuthorServiceClient client = new AuthorServiceClient();
MessageBox.Show(client.SayHellowWorld("gt"));