WebService(1)HelloWorld

一、新建Dynamic Web Project,命名为:webServiceServer

二、新建一个java类,命名为:HelloWebService,内容如下

package com.service;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class HelloWebService {
	
	public String getName(String name){
		return name+",你好呀";
	}

	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8083/Service/HelloWebService?wsdl", new HelloWebService());

	}

}
三、编译HelloWebService类,在浏览器中输入:http://localhost:8083/Service/HelloWebService?wsdl,显示内容如下:

--><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
<definitions targetNamespace="http://service.com/" name="HelloWebServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.com/" schemaLocation="http://localhost:8083/Service/HelloWebService?xsd=1"/>
</xsd:schema>
</types>
<message name="getName"><part name="parameters" element="tns:getName"/></message>
<message name="getNameResponse"><part name="parameters" element="tns:getNameResponse"/></message>
<portType name="HelloWebService">
<operation name="getName">
<input wsam:Action="http://service.com/HelloWebService/getNameRequest" message="tns:getName"/>
<output wsam:Action="http://service.com/HelloWebService/getNameResponse" message="tns:getNameResponse"/>
</operation>
</portType>
<binding name="HelloWebServicePortBinding" type="tns:HelloWebService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getName"><soap:operation soapAction=""/><input><soap:body use="literal"/>
</input><output><soap:body use="literal"/></output></operation></binding><service name="HelloWebServiceService">
<port name="HelloWebServicePort" binding="tns:HelloWebServicePortBinding"><soap:address location="http://localhost:8083/Service/HelloWebService"/></port>
</service></definitions>

四、新建一个Dynamic Web Project,命名为:webServiceClient

五、在命令行窗口输入:wsimport -s d:\\projcet\\webServiceClient\\src -p com.service.client -keep http://localhost:8083/Service/HelloWebService?wsdl
格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址”

六、刷新webServiceClient项目,检查生成类

七、创建Test测试类

package com.service.client;

public class Test {
	public static void main(String[] args) {
		HelloWebService service=new HelloWebServiceService().getHelloWebServicePort();
		System.out.println(service.getName("The first web Service"));
	}
}
运行测试类,输出结果:

The first web Service,你好呀
说明调用webServiceServer成功。
参考文献:
[1] http://www.cnblogs.com/yisheng163/p/4524808.html?utm_source=tuicool

你可能感兴趣的:(Web,service)