下面我们开始构建:
1,先用Eclipse建立一个动态网站,注意需要生成web.xml,建工程的时候不要忘记勾选;
2,建立类实现Web Services接口,下面是一个简单定义Web Services接口的实现类:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 1 package javaWebServiceTest; 2 3 import javax.jws.WebMethod; 4 import javax.jws.WebParam; 5 import javax.jws.WebResult; 6 import javax.jws.WebService; 7 import javax.jws.WebParam.Mode; 8 import javax.jws.soap.SOAPBinding; 9 import javax.jws.soap.SOAPBinding.Style; 10 import javax.jws.soap.SOAPBinding.Use; 11 12 import webServiceInterface.Person; 13 14 @WebService(name = " SayHello " , portName = " SayHellowServicePort " , serviceName = " SayHelloServiceName " , 15 targetNamespace = " http://www.webservice.com " ) 16 @SOAPBinding(style = Style.RPC,use = Use.LITERAL) 17 public class ChService { 18 19 @WebMethod(action = " sayHello " , operationName = " saySomeWord " ) 20 @WebResult(name = " Person " , partName = " PersonName " ) 21 public Person sayHelloWord(@WebParam(name = " paramA " , partName = " p1 " , mode = Mode.IN)String name){ 22 Person persons = new Person(); 23 persons.setName( " My name is " + name); 24 return persons; 25 } 26 27 } 28
3,Person类
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 1 package webServiceInterface; 2 3 4 public class Person 5 { 6 String name; 7 8 public String getName() { 9 return name; 10 } 11 12 public void setName(String name) { 13 this .name = name; 14 } 15 }
这样,我们基本上就实现了webservice的构建,是不是感觉不是很难,跟写普通的java方法一样,其实jdk自带的webservice是通过注释注入方法实现的。所以上面那些以"@"符号开头的都是非常重要的,也是必须的。
下面我们来配置我们写的WebService到Apache中:
1,上面第一步,我们生成了一个web.xml类,我们向<web-app>节点里面加入以下内容:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 1 < listener > 2 < listener - class > 3 com.sun.xml.ws.transport.http.servlet.WSServletContextListener 4 </ listener - class > 5 </ listener > 6 7 < servlet > 8 < servlet - name > TestService </ servlet - name > 9 < servlet - class > 10 com.sun.xml.ws.transport.http.servlet.WSServlet 11 </ servlet - class > 12 </ servlet > 13 14 < servlet - mapping > 15 < servlet - name > TestService </ servlet - name > 16 <url-pattern>/jdkService</url-pattern> 17 </ servlet - mapping >
2,在web.xml的目录中再建立一个sun-jaxws.xml来配置endpoint的相关内容。
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 1 <? xml version = " 1.0 " encoding = " UTF-8 " ?> 2 < endpoints xmlns = ' http://java.sun.com/xml/ns/jax-ws/ri/runtime ' version = ' 2.0 ' > 3 < endpoint 4 name = ' TestService ' 5 implementation = ' javaWebServiceTest.ChService ' 6 binding = " http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/ " 7 url - pattern = '/ jdkService ' /> 8 </ endpoints >
好了,配置完了。把我们建立的项目加入到apache7.0中,然后启动。
在浏览器输入:http://localhost:8080/TestWebService/jdkService?wsdl,便可以访问到。
如图:
现在我们来说说那些以"@"符号开头的内容。
1,@WebService(name="SayHello", portName="SayHellowServicePort", serviceName="SayHelloServiceName", targetNamespace="http://www.possystem.com")
name: SayHello
用户实例化用,例:
private WinService.SayHelloClient win = new winformService.WinService.SayHelloClient();
WSDL中:<portType name="SayHello">
serviceName: SayHelloServiceName
指定Web Service的名称 ,WSDL中:<service name="SayHelloServiceName">
portName: SayHellowServicePort
WSDL中:<port name="SayHellowServicePort" binding="tns:SayHellowServicePortBinding">
wsdlLocation:wsdlLocation 是引用预先存在的 WSDL 文件的 URL(相对或绝对)。
targetNamespace:wsdl命名空间
2,@SOAPBinding(style = Style.RPC,use = Use.LITERAL)
style: 定义发送到 Web Service 的消息和从 Web Service 发送的消息的编码样式。
Document和RPC两种形式
默认值:
javax.jws.soap.SOAPBinding.Style.DOCUMENT
use: 定义发送到 Web Service 的消息和从 Web Service 发送的消息的格式样式。
ENCODED和 LITERAL两种形式
默认值:
javax.jws.soap.SOAPBinding.Use.LITERAL
3, @WebMethod(action="sayHello", operationName="saySomeWord")
action: sayHello
此操作的动作。 对于 SOAP 绑定,此方法可确定 soap 动作的值。
WSDL中: <soap12:operation soapAction="sayHello" />
operationName: sayHelloWord1
与此方法匹配的 wsdl:operation 的名称。
此名称也用于客户端调用的方法的名称。
exclude
将某一方法标记为不作为一个 web 方法公开。
4, @WebResult(name="Person", partName="PersonName")
name: Person
返回值的名称。 如果操作是 rpc 样式,并且partName尚未指定, 表示此返回值的 wsdl:part 的名称
WSDL中:<part name="Person" type="tns:person" />
partName: PersonName
表示此返回值的 wsdl:part 的名称。
此名称只在操作是 rpc 样式,或者操作是文档样式且参数样式为 BARE 时使用。
WSDL中:<part name="PersonName" type="tns:person" />
5, public Person sayHelloWord(@WebParam(name="paramA", partName="p1", mode = Mode.IN)String name){……}
name: paramA
如果该操作是 rpc 样式的,并且尚未指定 @WebParam.partName,则此名称是表示参数的 wsdl:part 的名称。
如果该操作是文档样式的,或者参数映射到某一个头,则此名称是表示参数的 XML 元素的本地名称。
如果操作是文档样式的,参数样式为 BARE 并且模式为 OUT 或 INOUT,则必须指定一个名称。
客户端调用时参数名称显示该值。
partName: p1
表示此参数的 wsdl:part 的名称。
此名称仅在操作是 rpc 样式,或者操作是文档样式且参数样式为 BARE 时使用。
WSDL中:
<message name="sayHelloWord1">
<part name="p1" type="xsd:string" />
</message>
客户端调用时参数名称显示该值。
这个主要是根据样式来。
mode:参数的流向(IN、OUT 或 INOUT 之一)。
以上只是对一些常用的作了一些解释,都是自我理解的,有不正确的地方,还请指正。