0.新建一个项目取名wsserver. pom.xml 文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>sidonia</groupId><artifactId>wsserver</artifactId><version>0.0.1-SNAPSHOT</version><properties><cxf.version>3.1.4</cxf.version></properties><dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency></dependencies> </project>
1.新建一个interface 取名 HelloWorld
package com.test.hello;import javax.jws.WebService;@WebService public interface HelloWorld {public void sayHello(); }
2.新建一个实现类,取名HelloWorldImpl
package com.test.hello;import javax.jws.WebService;@WebService(endpointInterface="com.test.hello.HelloWorld",serviceName="HelloWorldWS") public class HelloWorldImpl implements HelloWorld{public void sayHello() {System.out.println("hello world");} }
3.新建一个包含main方法的ServerMain类
public class ServerMain {public static void main(String[] args) throws Exception {HelloWorld helloWorldImpl = new HelloWorldImpl();Endpoint.publish("http://localhost:1234/ws", helloWorldImpl);System.out.println("webservice 暴露成功");} }
4.这时候访问上面这个地址,就可以看见下面这个xml
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://hello.test.com/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"name="HelloWorldWS" targetNamespace="http://hello.test.com/"><wsdl:types><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:tns="http://hello.test.com/" elementFormDefault="unqualified"targetNamespace="http://hello.test.com/" version="1.0"><xs:element name="sayHello" type="tns:sayHello" /><xs:element name="sayHelloResponse" type="tns:sayHelloResponse" /><xs:complexType name="sayHello"><xs:sequence /></xs:complexType><xs:complexType name="sayHelloResponse"><xs:sequence /></xs:complexType></xs:schema></wsdl:types><wsdl:message name="sayHello"><wsdl:part element="tns:sayHello" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="sayHelloResponse"><wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part></wsdl:message><wsdl:portType name="HelloWorld"><wsdl:operation name="sayHello"><wsdl:input message="tns:sayHello" name="sayHello"></wsdl:input><wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"></wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="HelloWorldWSSoapBinding" type="tns:HelloWorld"><soap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http" /><wsdl:operation name="sayHello"><soap:operation soapAction="" style="document" /><wsdl:input name="sayHello"><soap:body use="literal" /></wsdl:input><wsdl:output name="sayHelloResponse"><soap:body use="literal" /></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="HelloWorldWS"><wsdl:port binding="tns:HelloWorldWSSoapBinding" name="HelloWorldImplPort"><soap:address location="http://localhost:1234/ws" /></wsdl:port></wsdl:service> </wsdl:definitions>
5.现在写客户端 取名wsclient
6.cmd 到D盘找个合适的地方 命令行执行 wsdl2java http://localhost:1234/ws?wsdl,这时候这里就会根据这个wsdl生成相应的类,把它考到客户端项目下面
7.新建一个ClientMain类
package com.test.hello;public class ClientMain {public static void main(String[] args) {HelloWorldWS factory = new HelloWorldWS(); //显然HelloWorldWS是一个工厂类HelloWorld helloWorld = factory.getHelloWorldImplPort();helloWorld.sayHello();} }
一运行,服务器端控制台就会打印出hello world