1. 基本库
cxf-2.2.4.jar
XmlSchema-1.4.5.jar
Wsdl4j-1.6.2.java
2. 在.NET中编写服务,默认参数即可,如:
namespace TestService2Java
{
[WebService(Namespace = "http://temp2java.org/")] //仅此处稍加修改,不改也可
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int MathAdd(int a, int b) //定义一个新方法
{
return a + b;
}
}
}
3. 生成Java中使用的类,使用wsdl2java:
wsdl2java –p test.net.service –d d:/src http://localhost/svr2java/Service1.asmx?wsdl
将生成文件中的Service1Soap.java导入java项目。
备注://@XmlSeeAlso({ObjectFactory.class}),否则调用会出错。
4. 调用服务:
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
public class CallDotNetService {
public final static QName SERVICE = new QName("http://temp2java.org/", "Service1");
public final static QName Service1Soap = new QName("http://temp2java.org/", "Service1Soap");
private CallDotNetService() { }
public static void main(String args[]) throws Exception {
URL wsdlURL = new URL("http://localhost/svr2java/Service1.asmx?wsdl");
Service srv = Service.create(wsdlURL, SERVICE);
Service1Soap port = srv.getPort(Service1Soap, Service1Soap.class);
System.out.println(port.helloWorld());
System.out.println(port.mathAdd(33, 44));
}
}