该示例采用的是非框架技术实现webService,即采用wsimport 创建
语法为:wsimport -s "src目录" -p "生成类所在包名称" -keep "wsdl发布地址"
注:src目录地址不可包含空格
wsdl发布地址不要遗漏了 ?wsdl
一、创建服务端
1、创建【web Service Project】,命名为【HelloServerProject】,注意这里的web工程是web Service project
2、在src下新建包com.cn.server,然后在包下创建class类HelloServer
3、在类HelloService中编写供客户端调用的方法sayHello()
package com.cn.server;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class HelloServer {
/**
* 供客户端调用的方法
* @param name 传入的参数
* @return String 返回结果
* */
public String sayHello(String name){
return "my name is "+name;
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:9001/Service/HelloServerProject", new HelloServer());
System.out.println("service success!");
}
}
注:使用@WebService将类HelloServer注解为webService类
使用Endpoint发布webService
4、测试结果
测试地址:http://localhost:9001/Service/HelloServerProject?wsdl
地址栏Service为固定的,HelloServiceProject为项目名称, ?wsdl为固定的页面
如果进入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.4-b01. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
-<definitions name="HelloServerService" targetNamespace="http://server.cn.com/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://server.cn.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
-<types>
-<xsd:schema>
<xsd:import schemaLocation="http://localhost:9001/Service/HelloServerProject?xsd=1" namespace="http://server.cn.com/"/>
</xsd:schema>
</types>
-<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
-<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
-<portType name="HelloServer">
-<operation name="sayHello">
<input message="tns:sayHello" wsam:Action="http://server.cn.com/HelloServer/sayHelloRequest"/>
<output message="tns:sayHelloResponse" wsam:Action="http://server.cn.com/HelloServer/sayHelloResponse"/>
</operation>
</portType>
-<binding name="HelloServerPortBinding" type="tns:HelloServer">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
-<operation name="sayHello">
<soap:operation soapAction=""/>
-<input>
<soap:body use="literal"/>
</input>
-<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
-<service name="HelloServerService">
-<port name="HelloServerPort" binding="tns:HelloServerPortBinding">
<soap:address location="http://localhost:9001/Service/HelloServerProject"/>
</port>
</service>
</definitions>
二、生成客户端
1、创建【web Service Project】, 命名为【HelloClientProject】
2、打开cmd窗口,执行如下命令:
1)、打开cmd窗口
2)、执行命令生成类
wsimport -s G:\\workPlace1\\HelloClientProject\\src -p com.cn.client -keep http://localhost:9001/Service/HelloServerProject?wsdl
3、刷新项目,检查生成的类
三、测试
1、在【HelloClientProject】的src下新建包com.cn.test,然后在该包下创建class类HelloServiceTest
package com.cn.test;
public class HelloServerTest {
}
2、在HelloServiceTest类下编写测试方法,调用服务端方法
package com.cn.test;
import com.cn.client.HelloServer;
import com.cn.client.HelloServerService;
public class HelloServerTest {
public static void main(String[] args) {
//初始化对象
HelloServer helloServer=new HelloServerService().getHelloServerPort();
//调用对象的方法
String name=helloServer.sayHello("张三");
System.out.println(name);
}
}