schema和http我们都了解的差不多了,下面我们就谈谈发布webservice和用jdk生成客户端代码以及使用Eclipse的监听;
1.启动webservice
这里我们用jdk自带的功能
helloWS.java
package com.atguigu.day01_ws.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
/*
* SEI:
*/
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);
}
helloWSImpl.java
package com.atguigu.day01_ws.ws;
import javax.jws.WebService;
/*
* SEI
*/
@WebService
public class HelloWSImpl implements HelloWS {
@Override
public String sayHello(String name) {
System.out.println("server sayHello()"+name);
return "goodNight " +name;
}
}
这里@webService 和@WebMethod注解分别对应的是类和方法,这里简单的我们以及写好,下面写发布测试类
test.java
package com.atguigu.day01_ws.ws.server;
import javax.xml.ws.Endpoint;
import com.atguigu.day01_ws.ws.HelloWSImpl;
/*
* 发布Web Service
*/
public class ServerTest {
public static void main(String[] args) {
String address = "http://localhost:8989/day01_ws/hellows";
Endpoint.publish(address , new HelloWSImpl());
System.out.println("发布webservice成功!");
}
}
这时候启动,我们会发现以及启动成功,用网页打开
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><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://ws.day01_ws.atguigu.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.day01_ws.atguigu.com/" name="HelloWSImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.day01_ws.atguigu.com/" schemaLocation="http://localhost:8989/day01_ws/hellows?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="HelloWSImpl">
<operation name="sayHello">
<input wsam:Action="http://ws.day01_ws.atguigu.com/HelloWSImpl/sayHelloRequest" message="tns:sayHello"></input>
<output wsam:Action="http://ws.day01_ws.atguigu.com/HelloWSImpl/sayHelloResponse" message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloWSImplPortBinding" type="tns:HelloWSImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWSImplService">
<port name="HelloWSImplPort" binding="tns:HelloWSImplPortBinding">
<soap:address location="http://localhost:8989/day01_ws/hellows"></soap:address>
</port>
</service>
</definitions>
http://localhost:8989/day01_ws/hellows?wsdl地址查看如上
这是发布后的xml信息,分为五大结构,后面会介绍
2.用jdk的工具生成客户端代码
这就是jdk下bin文件夹下的工具,可以用它生成客户端代码
新建一个工程,运行cmd,进入dos命令下,cd (地址) 地址是新建工程的src目录,也就是 生成客户端代码的地方,然后运行
wsimport -keep (wsdl地址) 这里的地址可以网上免费的wsdl地址。也可以是你自己写的wsdl地址,运行后,刷新目录。
这里就是生成的代码,这里就需要我们写一个测试类来测试了
clientTest.java
<pre name="code" class="java">package com.atguigu.day01_ws.ws_server;
import com.atguigu.day01_ws.ws.HelloWSImpl;
import com.atguigu.day01_ws.ws.HelloWSImplService;
/**
*调用webservice
* @author yb
*
*/
public class ClientTest {
public static void main(String[] args) {
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWS = factory.getHelloWSImplPort();
System.out.println(helloWS);
System.out.println(helloWS.getClass());
String result = helloWS.sayHello("yb");
System.out.println("client: "+result);
}
}
运行后,
<pre name="code" class="java"><img src="https://img-blog.csdn.net/20140831190829617?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWliaW5nNTQ4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
这就表示发布成功了。
3.监听,这里我就不多讲了,我介绍下