Myeclipse9.X 搭建 axis2 WebService
SetupWebService.java
/**
* WebService.java
* com.lewking.service
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────────
* Ver 1.1 2011-6-9 lewking
*
* Copyright (c) 2011, TNT All Rights Reserved.
*/
package com.lewking.service;
import org.apache.log4j.Logger;
/**
* ClassName:WebService
* Function: TODO ADD FUNCTION
* Reason: TODO ADD REASON
*
* @author lewking
* @version
* @since Ver 1.1
* @Date 2011-6-9 下午09:44:41
*
* @see
* @ deprecated
*/
public class SetupWebService {
Logger logger = Logger.getLogger(SetupWebService.class.getName());
private String msg;
public String welcome(String t){
logger.info("---" + t + msg);
return t + msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="lewkingService" class="com.lewking.service.SetupWebService">
<property name="msg">
<value>Hello World</value>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet>
<display-name>
Apache-Axis Admin Servlet Web Admin</display-name>
<servlet-name>AxisAdminServlet</servlet-name>
<servlet-class>
org.apache.axis2.transport.http.AxisAdminServlet</servlet-class>
<load-on-startup>100</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>AxisAdminServlet</servlet-name>
<url-pattern>/axis2-admin/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>/axis2-web/index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/axis2-web/Error/error404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/axis2-web/Error/error500.jsp</location>
</error-page>
<servlet>
<display-name>Axis Admin Servlet</display-name>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
<!-- spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
在web-inf 下建 services/webservice/META-INF/services.xml 内容如下
<!--web service 名称-->
<service name="lewking" targetNamespace="http://service.lewking.com/">
<Description>
Please Type your service description here
</Description>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<!-- spring bean Id-->
<parameter name="SpringBeanName" locked="false">lewkingService</parameter>
<!-- webservice 类的完全路径-->
<parameter name="ServiceClass" locked="false">com.lewking.service.SetupWebService</parameter>
<messageReceivers>
<!--表示有参没有返回值的方法-->
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<!--表示有参有返回值的方法-->
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<excludeOperations>
<operation></operation>
</excludeOperations>
</service>
如果启动时遇到异常:
- The service cannot be found for the endpoint reference (EPR) /webservices/services
org.apache.axis2.AxisFault: The service cannot be found for the endpoint reference (EPR) /webservices/services
at org.apache.axis2.engine.DispatchPhase.checkPostConditions(DispatchPhase.java:65)
at org.apache.axis2.engine.Phase.invoke(Phase.java:333)
at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:264)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:163)
at org.apache.axis2.transport.http.util.RESTUtil.invokeAxisEngine(RESTUtil.java:136)
at org.apache.axis2.transport.http.util.RESTUtil.processURLRequest(RESTUtil.java:130)
at org.apache.axis2.transport.http.AxisServlet$RestRequestProcessor.processURLRequest(AxisServlet.java:829)
at org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:255)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
解决:
把 WEB-INF 下的index.jsp 删除, 将 axis2.war 里的 axis2-web 目录拷贝到 webroot 目录下,就OK
Test Code:
/**
* WebServiceTest.java
* com.lewking.test
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────────
* Ver 1.1 2011-6-8 lewking
*
* Copyright (c) 2011, TNT All Rights Reserved.
*/
package com.lewking.test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.junit.Test;
/**
* ClassName:WebServiceTest
* Function: TODO ADD FUNCTION
* Reason: TODO ADD REASON
*
* @author lewking
* @version
* @since Ver 1.1
* @Date 2011-6-8 下午11:24:34
*
* @see
* @ deprecated
*/
public class WebServiceTest {
@Test
public void test() throws ServiceException, RemoteException, MalformedURLException{
String endpoint ="http://localhost:8080/webservices/services/lewking?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("http://service.lewking.com","welcome"));
String ret = (String) call.invoke(new Object[] {"Samland"});
System.out.println("Sent 'Hello!', got '" + ret + "'");
}
}