天天看点

使用JDK发布一个简单WebService

1.名词解析

WSDL – WebService Description Language – Web服务描述语言。

SOAP-Simple Object Access Protocol(简单对象访问协议)。

SOA- service-oriented architecture  面向服务的架构。

2.发布一个WebService服务类的前提:

   1.JDK的版本必须是1.6.0_21以及以上版本。

   2.类的注解必须是@WebService。

   3.至少包含一个实例方法,static、final方法不对外提供服务,如果没有一个可用的实例的方法,则会抛出如下异常

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

最简单的一个WebService服务类HelloService

package com.service.jdk;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * @author jackphang
 * @date 2013-4-11
 * @description 将一个类发布出去,JDK必须是1.6.0_21及以上
 */
/**
 * 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service
 * 接口。必须要有此注解,才能发布,否则会抛出异常 java.lang.IllegalArgumentException: class
 * com.pyy.ws.HelloService has neither @WebSerivce nor @WebServiceProvider
 * annotation wsdl: name:自定义的服务的名字,serviceName:获取自定义服务的服务的名字,
 * targetNamespace:名称空间,默认值是包结构的反义
 */
@WebService(name = "MyHelloService", portName = "MyHelloService", serviceName = "MyHelloServiceService", targetNamespace = "com.pyy.mypackage")
public class HelloServiceForJDK {

	// 对外发布服务的网络地址
	private final static String address = "http://localhost:1111/hello";

	// wsdl:修改方法的名字
	@WebMethod(operationName = "mySayHello")
	// wsdl:修改return 的名字
	@WebResult(name = "myReturn")
	// wsdl:修改参数的名字
	public String sayHello(@WebParam(name = "str") String str,
			@WebParam(name = "age") int age) {
		System.out.println("有人说:" + str);
		return str;
	}

	@WebMethod(exclude = true)
	public void exclude() {
		System.out.println("此注解表示方法不对外提供服务");
	}

	public static void staticMethod() {
		System.out.println("static 方法不对外提供服务");
	}

	public final void finalMethod() {
		System.out.println("final 方法不对外提供服务");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * 参数一:终端服务所在网络路径 ,参数二:被发布的实现者,当前实例对象 将该类作为服务对外发布出去。
		 */
		Endpoint.publish(address, new HelloServiceForJDK());
	}

}
           

如果不想方法对外提供服务,则可以在方法前加上注解@WebMethod(exclude = true) 如:

@WebMethod(exclude = true)
	public void exclude() {
		System.out.println("此注解表示方法不对外提供服务");
	}
           

 写完类之后,运行程序,打开浏览器,在地址栏输入http://localhost:1111/hello?wsdl  回车,你就会看到刚刚你写的服务类的使用说明,也就是WSDL。

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="MyHelloServiceService" targetNamespace="com.pyy.mypackage" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="com.pyy.mypackage" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:types>
<xs:schema elementFormDefault="unqualified" targetNamespace="com.pyy.mypackage" version="1.0" xmlns:tns="com.pyy.mypackage" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mySayHello" type="tns:mySayHello"/>
<xs:element name="mySayHelloResponse" type="tns:mySayHelloResponse"/>
<xs:complexType name="mySayHello">
<xs:sequence>
<xs:element minOccurs="0" name="str" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="mySayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="myReturn" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="mySayHelloResponse">
    <wsdl:part element="tns:mySayHelloResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="mySayHello">
    <wsdl:part element="tns:mySayHello" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <!--自己写的服务的类-->
  <wsdl:portType name="MyHelloService">
  	<!--方法-->
    <wsdl:operation name="mySayHello">
    	<!--输入参数-->
      <wsdl:input message="tns:mySayHello" name="mySayHello">
    </wsdl:input>
    <!--返回值-->
      <wsdl:output message="tns:mySayHelloResponse" name="mySayHelloResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="MyHelloServiceServiceSoapBinding" type="tns:MyHelloService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="mySayHello">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="mySayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="mySayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <!--获取服务的类-->
  <wsdl:service name="MyHelloServiceService">
  	<!--自己写的服务的类-->
    <wsdl:port binding="tns:MyHelloServiceServiceSoapBinding" name="MyHelloService">
    	<!--访问服务的地址-->
      <soap:address location="http://localhost:1111/hello"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
           

以上的WSDL是不是没有看到static 、 final 、以及注解为@WebMethod(exclude = true)的方法,说明它们不对外提供服务。

好了。第一个简单的WebService也就对外发布成功了。如何使用我们刚刚发布的服务请查看 如何使用WebService服务

继续阅读