Java6自带的WebService

只需要在一个类上标注@WebService就可以了,当然这个类需要有服务方法,否则会报下面的错误:

Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: The web service defined by the class java6.webservice.HelloWorldService does not contain any valid WebMethods.

at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(Unknown Source)

at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(Unknown Source)

at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(Unknown Source)

at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)

at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)

at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(Unknown Source)

at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source)

at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source)

at javax.xml.ws.Endpoint.publish(Unknown Source)

at java6.webservice.HelloWorldService.main(HelloWorldService.java:13)

 

再使用Endpoint.publish()方法发布服务即可。

示例:

import javax.jws.WebService;

import javax.xml.ws.Endpoint;

 

@WebService

publicclass HelloWorldService {

 

public String sayHello(String name) {

return"hello" + name;

}

 

/**

* @param args

*/

publicstaticvoid main(String[] args) {

Endpoint.publish("http://localhost:8080/java6/webservice/HelloWorldService", new HelloWorldService());

}

 

}

这样服务端就写好了。

Endpoint.publish是一个后台线程,一旦发布了就会一直等待处理Web服务的请求。

在浏览器中输入http://localhost:8080/java6/webservice/HelloWorldService?wsdl,就可以看到发布的服务了。

<?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.6 in JDK 6. 

-->

- <!--

Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 

-->

- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.java6/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.java6/" name="HelloWorldServiceService">

- <types>

- <xsd:schema>

<xsd:import namespace="http://webservice.java6/" schemaLocation="http://localhost:8080/java6/webservice/HelloWorldService?xsd=1" />

</xsd:schema>

</types>

- <message name="sayHello">

<part name="parameters" element="tns:sayHello" />

</message>

- <message name="sayHelloResponse">

<part name="parameters" element="tns:sayHelloResponse" />

</message>

- <portType name="HelloWorldService">

- <operation name="sayHello">

<input message="tns:sayHello" />

<output message="tns:sayHelloResponse" />

</operation>

</portType>

- <binding name="HelloWorldServicePortBinding" type="tns:HelloWorldService">

<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />

- <operation name="sayHello">

<soap:operation soapAction="" />

- <input>

<soap:body use="literal" />

</input>

- <output>

<soap:body use="literal" />

</output>

</operation>

</binding>

- <service name="HelloWorldServiceService">

- <port name="HelloWorldServicePort" binding="tns:HelloWorldServicePortBinding">

<soap:address location="http://localhost:8080/java6/webservice/HelloWorldService" />

</port>

</service>

</definitions>

 

 

 

客户端:

使用jdkwsimport命令生成客户端代码:

Java6自带的WebService_第1张图片

Java6自带的WebService_第2张图片

编写我们调用服务的类:

 

import java6.webservice.client.*;

publicclass HelloWorldClient {

 

/**

* @param args

*/

publicstaticvoid main(String[] args) {

// TODO Auto-generated method stub

HelloWorldServiceService hwss = new HelloWorldServiceService();

java6.webservice.client.HelloWorldService hws = hwss.getHelloWorldServicePort();

String result = hws.sayHello("webservice");

System.out.println(result);

}

 

}

 

 

运行结果:hellowebservice

 

 

你可能感兴趣的:(java,webservice)