新建一个web或java工程(maven工程也可以)
创建一个类,使用Endpoint方式发布
package com.jsp.web;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class HelloWorld {
@WebMethod
public String sayHello(String greet) {
System.out.println("get Message...");
String result = "Hello World, " + greet;
return result;
}
public static void main(String[] args) {
System.out.println("server is running");
String address = "http://localhost:8089/service/HelloWorld";
HelloWorld implementor = new HelloWorld();
Endpoint.publish(address, implementor);
System.out.println("published !");
}
}
启动main方法,完成发布
浏览器访问http://localhost:8089/service/HelloWorld
坑!
使用jdk1.8不能访问,百度好久才知道是jdk问题,使用jdk1.7就可以了,具体原因还不知道,暂且几下
webService客户端开发
新建
输入WSDL服务描述文件的访问地址
http://localhost:8089/service/HelloWorld?wsdl
生成代码
编写测试类
**package com.jsp;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.jsp.web.HelloWorld;
import com.jsp.web.HelloWorldServiceLocator;
public class TestHelloWorld {
public static void main(String[] args) throws ServiceException, RemoteException {
// 1.创建Locator对象,相当于传统服务的类或者是库
HelloWorldServiceLocator hsLocator = new HelloWorldServiceLocator();
// 2.获取port,相当于传统类或库的方法接口
HelloWorld hPort = hsLocator.getHelloWorldPort(); // ServiceException
// 3.通过port调用服务
String sayHello = hPort.sayHello("I love the whole world!"); // RemoteException
System.out.println(sayHello);
}
}**
运行
学习知识总结
基本的WebService平台是Http+Xml
WebService三大基本元素是WSDL,SOAP,UDDI
WSDL核心是XML Schema(与DTD相类似)
SOAP = HTTP + XML
UDDI是构建唯一web服务的框架
WSDL文档结构
元素 | 定义 |
---|---|
<portType> | web service 执行的操作 |
<message> | web service 使用的消息 |
<types> | web service 使用的数据类型 |
<binding> | web service 使用的通信协议 |
一个 WSDL 文档的主要结构是类似这样的
<definitions>
<types>
definition of types........
</types>
<message>
definition of a message....
</message>
<portType>
definition of a port.......
</portType>
<binding>
definition of a binding....
</binding>
</definitions>
SOAP 构建模块
基本结构
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
...
...
</soap:Header>
<soap:Body>
...
...
<soap:Fault>
...
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
关系wsdl,soap,uddi相关介绍可以参考
http://www.w3school.com.cn/ws.asp