web系统中通常我们需要为其他系统提供webservice服务,其中依赖jar包文件不做详细描述。
现在就基本配置步骤总结如下:
配置1:在web.xml文件中增加如下代码。
<!-- 配置websercice服务的servlet -->
?<servlet>
??<servlet-name>AxisServlet</servlet-name>
??<servlet-class>
???org.apache.axis.transport.http.AxisServlet
??? </servlet-class>
?</servlet>
?<servlet>
??<servlet-name>AdminServlet</servlet-name>
??<servlet-class>
???org.apache.axis.transport.http.AdminServlet
??? </servlet-class>
??<load-on-startup>1</load-on-startup>
?</servlet>
?<servlet>
??<servlet-name>SOAPMonitorService</servlet-name>
??<servlet-class>
???org.apache.axis.monitor.SOAPMonitorService
??? </servlet-class>
??<init-param>
???<param-name>SOAPMonitorPort</param-name>
???<param-value>5001</param-value>
??</init-param>
??<load-on-startup>2</load-on-startup>
?</servlet>
?<servlet-mapping>
??<servlet-name>AxisServlet</servlet-name>
??<url-pattern>/servlet/AxisServlet</url-pattern>
?</servlet-mapping>
?<servlet-mapping>
??<servlet-name>AxisServlet</servlet-name>
??<url-pattern>*.jws</url-pattern>
?</servlet-mapping>
?<servlet-mapping>
??<servlet-name>AxisServlet</servlet-name>
??<url-pattern>/services/*</url-pattern>
?</servlet-mapping>
?<servlet-mapping>
??<servlet-name>SOAPMonitorService</servlet-name>
??<url-pattern>/SOAPMonitor</url-pattern>
?</servlet-mapping>
?
配置2:在WEB-INF目录下增加server-config.wsdd文件,其中内容为我们需要发布的service服务对象。
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
?<globalConfiguration>
? <parameter name="sendMultiRefs" value="true"/>
? <parameter name="disablePrettyXML" value="true"/>
? <parameter name="adminPassword" value="admin"/>
? <parameter name="dotNetSoapEncFix" value="true"/>
? <parameter name="enableNamespacePrefixOptimization" value="true"/>
? <parameter name="sendXMLDeclaration" value="true"/>
? <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
? <parameter name="sendXsiTypes" value="true"/>
? <requestFlow>
?? <handler type="java:org.apache.axis.handlers.JWSHandler">
??? <parameter name="scope" value="session"/>
?? </handler>
?? <handler type="java:org.apache.axis.handlers.JWSHandler">
??? <parameter name="scope" value="request"/>
??? <parameter name="extension" value=".jwr"/>
?? </handler>
? </requestFlow>
?</globalConfiguration>
?<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
?<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
?<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
?
?<transport name="http">
? <requestFlow>
?? <handler type="URLMapper"/>
?? <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
? </requestFlow>
? <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
? <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
? <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
? <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
?</transport>
?<transport name="local">
? <responseFlow>
?? <handler type="LocalResponder"/>
? </responseFlow>
?</transport>
<service name="UserWebService" type="" regenerateElement="true"
??? provider="java:RPC" style="rpc" use="encoded">
??? <parameter name="scope" value="Request" regenerateElement="false"/>
??? <parameter name="className" value="com.demo.webservice.UserWebService" regenerateElement="false"/>
??? <parameter name="allowedMethods" value="getUserNameById" regenerateElement="false"/>
</service>
?
完成上述的两个步骤即可完成werbservice的配置,但是具体的service的实现类。根据各自需求完成,这里贴出一部分简要代码
package com.demo.webservice;
/**
?* <p> Title: 用户信息的webservice测试类</p>
?*/
public class UserWebService extends ServletEndpointSupport implements IUserWebService{
?
//注意,此处不能通过ioc的方式进行依赖注入,只能通过下面的方式获取到userService的对象。然后进行service层调用(前提是getBean("userService")的userService已经在spring的配置文件中进行了配置)
?private IUserService userService;
?protected void onInit()
?{
??userService =(IUserService)getApplicationContext().getBean("userService");
?}
?
?public String getUserNameById(String userId)
?{
??System.out.println("getUserNameById=123==" + userId);
??String userName="fafa";
??User user = userService.findUserById(userId);
??userName = user.getUsername();
??System.out.println("getUserNameById=123==userName:" +userName );
??return userName;
?}
}
?
?
注意:由于我们的UserWebService 应用了spring的依赖注入,所以其中多了一项public void setUserService的方法,在发布时通过http://localhost:8080/sshdemo/services/UserWebService?wsdl查看生成的wsdl文件时会报错:Namespace ? prefixes ? must ? be ? set ? on ? the ? Definition ? object ? using ? the ? addNamespace(...) ? method.: ?
? faultActor: ?
? faultNode: ?
? faultDetail:??
?
,而我们在发布websercice的时候配置文件中allowedMethods的value值应该只指定我们需要暴漏的方法getUserNameById,而不能用*号代替,否则会出错。
?
一个简单的webservice应用就配置发布好了。通过上面的链接可以查看到生成的xml文件即表示成功。祝你好运。
?
??
?