首先创建一个空的项目:
第二步:创建webservice模块
@WebService()
public class HelloWorld{
@WebMethod(operationName = "sayHelloWorldFrom")
public String sayHelloWorldFrom(String from) {
String result = "Hello, world, from " + from;
System.out.println(result);
return result;
}
@WebMethod(operationName = "sayHelloWorldFrom1")
public String sayHelloWorldFrom1(String from,String s) {
String result = "Hello, world " + from;
System.out.println(result);
return result;
}
public static void main(String[] argv) {
Object implementor = new HelloWorld();
String address = "http://localhost:8989/HelloWorld";
//把他放进服务器中
Endpoint.publish(address, implementor);
System.out.println("发布成功" + address);
}
}
然后在你调用的类里面连接号这个小型服务器,最重要的是地址和端口号要一样。
1:是调用的服务器的地址
2:是电用的项目地址
3:是生产mebservice项目的目录
之后会生成一系列的类:
HelloWorld这个接口是被调用类里已被实现的接口。
HelloWorld.wsdl的解释
<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.7-b01 svn-revision#${svn.Last.Changed.Rev}. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.7-b01 svn-revision#${svn.Last.Changed.Rev}. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://example/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example/" name="HelloWorldService">
<types>
<xsd:schema>
<xsd:import namespace="http://example/" schemaLocation="http://localhost:9000/HelloWorld?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHelloWorldFrom">
<part name="parameters" element="tns:sayHelloWorldFrom"/>
</message>
<message name="sayHelloWorldFromResponse">
<part name="parameters" element="tns:sayHelloWorldFromResponse"/>
</message>
<portType name="HelloWorld">
//方法名称
<operation name="sayHelloWorldFrom">
//说明有接收参数
<input wsam:Action="http://example/HelloWorld/sayHelloWorldFromRequest" message="tns:sayHelloWorldFrom"/>
//说明有返回值
<output wsam:Action="http://example/HelloWorld/sayHelloWorldFromResponse" message="tns:sayHelloWorldFromResponse"/>
</operation>
</portType>
//真正实现改ws的名称不重要 type:实现此服务的真实类型
<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
//transport:说明此服务soap服务
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
//operation 说明此服务可以被调用的方法
<operation name="sayHelloWorldFrom">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
//service name :制定ws服务名称,通过此名称获取ws服务
<service name="HelloWorldService">
port name:此服务下面提供的服务端口
<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
//address location:服务的访问地址,如果要看wsdl,则在此地址后面添加wsdl
<soap:address location="http://localhost:9000/HelloWorld"/>
</port>
</service>
</definitions>