如何建立一个WebService项目
(1) 首先新建一个项目,导入Spring和axis包
(2) 修改web.xml文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value></context-param><listener> <display-name>contextConfigLocation</display-name> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener><servlet> <servlet-name>axis</servlet-name> <servlet-class> org.apache.axis.transport.http.AxisServlet </servlet-class> <load-on-startup>2</load-on-startup></servlet><servlet-mapping> <servlet-name>axis</servlet-name> <url-pattern>/ws/*</url-pattern></servlet-mapping>
<bean id="MathBean" name="code"><?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="adminPassword" value="admin"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="sendMultiRefs" value="true"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" 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="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <!-- 需要修改的配置start --> <service name="MathService" provider="java:RPC"> <parameter name="allowedMethods" value="*"/> <parameter name="className" value="test.remote.JaxRpcMathService"/> <beanMapping qname="wsdemo:MathVO" xmlns:wsdemo="urn:wsdemo" languageSpecificType="java:test.vo.MathVO"/> </service> <!-- 需要修改的配置end --> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService"/> <parameter name="enableRemoteAdmin" value="false"/> <parameter name="className" value="org.apache.axis.utils.Admin"/> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <service name="Version" provider="java:RPC"> <parameter name="allowedMethods" value="getVersion"/> <parameter name="className" value="org.apache.axis.Version"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </requestFlow> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder"/> </responseFlow> </transport></deployment>
public class JaxRpcMathService extends ServletEndpointSupport implements MathService { private MathService mathService; protected void onInit() { this.mathService = (MathService) getWebApplicationContext().getBean(MathService.BEAN_ID); } public int add(int x, int y) { return mathService.add(x,y); } public int subtract(int x, int y) { return mathService.subtract(x,y); } public MathVO testVO(MathVO vo) { return mathService.testVO(vo); }}
public interface MathService { String BEAN_ID = "MathBean"; public int add(int x, int y); public int subtract(int x, int y); public MathVO testVO(MathVO vo);}
public class MathServiceImpl implements MathService{ public int add(int x, int y) { return x+y; } public int subtract(int x, int y) { return x-y; } public MathVO testVO(MathVO vo) { System.out.println(vo.toString()); return vo; }}
public class TestMathService { public static void main(String[] args) { JaxRpcMathServiceService service = new JaxRpcMathServiceServiceLocator(); JaxRpcMathService client = service.getMathService(); System.out.println(client.add(1, 2)); }}