创建一个项目
添加如下代码
package jx;
import javax.jws.WebService;
@WebService
public class ItemService implements ItemServiceAPI{
public String sayHello() {
return "HelloWorld";
}
}
package jx;
import javax.jws.WebService;
@WebService
public interface ItemServiceAPI {
public String sayHello();
}
以上是一个接口和一个实现类
下面写一个Main方法
package jx;
import javax.xml.ws.Endpoint;
public class Main {
public static void main(String[] args) {
ItemServiceAPI impl=new ItemService();
Endpoint.publish("http://192.168.0.119:8999/ItemService", impl);
}
}
接下来运行,然后访问浏览器:
http://192.168.0.119:8999/ItemService?wsdl
<?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://jx/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jx/" name="ItemServiceService"> <types> <xsd:schema> <xsd:import namespace="http://jx/" schemaLocation="http://192.168.0.119:8999/ItemService?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="ItemService"> <operation name="sayHello"> <input wsam:Action="http://jx/ItemService/sayHelloRequest" message="tns:sayHello"></input> <output wsam:Action="http://jx/ItemService/sayHelloResponse" message="tns:sayHelloResponse"></output> </operation> </portType> <binding name="ItemServicePortBinding" type="tns:ItemService"> <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="ItemServiceService"> <port name="ItemServicePort" binding="tns:ItemServicePortBinding"> <soap:address location="http://192.168.0.119:8999/ItemService"></soap:address> </port> </service> </definitions>
这时使用jdk自带工具生成客户端
wsimport -keep -s . http://192.168.0.119:8999/ItemService?wsdl
目录下出现一个jx文件夹,里面是生成的源码ItemService,ItemServiceService,ObjectFactory,package-info,SayHello,SayHelloResponse
新建另一个项目,把jx代码复制进去
创建Main方法
package root;
import jx.ItemServiceService;
public class Main {
public static void main(String[] args) {
ItemServiceService m=new ItemServiceService();
String stm=m.getItemServicePort().sayHello();
System.out.println(stm);
}
}
执行后,输出HelloWorld
ItemServiceService.java中涉及到地址硬编码的可以抽出,配置。