来个简单点的:
1.建个具体的服务实现:
package com.webservice;
@WebService
public class Warehouse {
private Map<String, Double> prices;
public Warehouse() {
prices = new HashMap<String, Double>();
prices.put("Blackwell Toaster", 24.95);
prices.put("ZapXpress Microwave Oven", 49.95);
}
public double getPrice(@WebParam(name = "description")
String description) {
Double price = prices.get(description);
return price == null ? 0 : price;
}
}
2。生成存根(stub) 的classes,在RMI中这会自动生成。WebService中可用JAX-WS工具生成:
进到工程的classes目录下:
wsgen -classpath . com.webservice.Warehouse
在com.webservice.jaxws中会生成一些class类,在这里的是GetPrice.class和GetPriceResponse.class
其实也就是把参数和返回值包装的类。
3。是时候部署了,在这里,我们用jdk提供的简单机制:
package com.webservice;
public class WarehouseServer {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/WebService/warehouse", new Warehouse());
}
}
运行后,在浏览器里敲上上面的地址 http://localhost:8080/WebService/warehouse?wsdl 就可以看到WebService提供的信息:
<?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.1.1 in JDK 6.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6.
-->
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.com/" name="WarehouseService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://webservice.com/" schemaLocation="http://localhost:8080/WebService/warehouse?xsd=1" />
</xsd:schema>
</types>
- <message name="getPrice">
<part name="parameters" element="tns:getPrice" />
</message>
- <message name="getPriceResponse">
<part name="parameters" element="tns:getPriceResponse" />
</message>
- <portType name="Warehouse">
- <operation name="getPrice">
<input message="tns:getPrice" />
<output message="tns:getPriceResponse" />
</operation>
</portType>
- <binding name="WarehousePortBinding" type="tns:Warehouse">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="getPrice">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
</binding>
- <service name="WarehouseService">
- <port name="WarehousePort" binding="tns:WarehousePortBinding">
<soap:address location="http://localhost:8080/WebService/warehouse" />
</port>
</service>
</definitions>
4。生成所需的类。
同样,利用JAX-WS工具,生成我们要用的类:
进到工程的src目录下:
wsimport -keep -p com.webservice.server http://localhost:8080/WebServices/warehouse?wsdl
注意:包的名字和用wsgen时的不一样。
在com.webservice.server下会生成我们需要的类:
GetPrice
GetPriceResponse
Warehouse (接口)
package-info
WarehouseService
ObjectFactory
5。远程调用服务
写个客户端:
package com.webservice;
public class Client {
public static void main(String[] args) {
WarehouseService service = new WarehouseService();
Warehouse port = service.getPort(Warehouse.class);
String descr = "Blackwell Toaster";
double price = port.getPrice(descr);
System.out.println(descr + ": " + price);
}
}
6。总结:
主要用到了JAX-WS这个工具,wsgen,wsimport的用法。了解wsdl文件所提供的服务器信息,就可以远程相拥WebService服务了。