第一步:确认你用到的spring bean存在并且有效 比如 inviteService
第二步:创建要发布的服务方法
/**
*
*/
package com.chinamobile.survey.webservice;
import java.util.List;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.chinamobile.survey.entity.vo.ExamPlanContainer;
import com.chinamobile.survey.invite.business.IInviteService;
/**
* @author jianqiang.jiang
*
*/
public class InviteEndPoint extends ServletEndpointSupport implements
IInviteService {
// 将真实的业务bean包装成WebService
private IInviteService inviteService;
// 该方法由Spring调用,将目标业务bean注入。
protected void onInit() {
this.inviteService = (IInviteService) getWebApplicationContext()
.getBean("inviteService");
}
// 将业务bean的业务方法暴露成WebService
public int getAllowAnswerExamPlanCount(long userId) throws Exception {
return inviteService.getAllowAnswerExamPlanCount(userId);
}
public ExamPlanContainer getAllowAnswerExamPlanContainer(long userId, long offset, long maxRow)
throws Exception {
return inviteService.getAllowAnswerExamPlanContainer(userId, offset, maxRow);
}
}
注意里面的 ServletEndpointSupport 和对bean的引用
第三步.编辑两个文件
1.web.xml中
增加
<!-- 定义AxisServlet-->
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<!-- 映射AxisServlet,使用通配符-->
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
2.生成server-config.wsdd,放在WEB-INF下
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper"
type="java:org.apache.axis.handlers.http.URLMapper" />
<service name="ExamService" provider="java:RPC">
<parameter name="className"
value="com.chinamobile.survey.webservice.InviteEndPoint" />
<parameter name="allowedMethods"
value="getAllowAnswerExamPlanCount,getAllowAnswerExamPlanContainer" />
<parameter name="allowedRoles" value="user1,user2" />
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
</requestFlow>
</transport>
<beanMapping qname="myNS:ExamPlanContainer"
xmlns:myNS="urn:BeanService"
languageSpecificType="java:com.chinamobile.survey.entity.vo.ExamPlanContainer" />
<beanMapping qname="myNS:WsExamPlan" xmlns:myNS="urn:BeanService"
languageSpecificType="java:com.chinamobile.survey.entity.vo.WsExamPlan" />
</deployment>
注意其中的 返回类型注册,
第四步.
生成客户端测试代码:
package com.chinamobile.survey.webservice;
import java.util.Iterator;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import com.chinamobile.survey.entity.vo.ExamPlanContainer;
import com.chinamobile.survey.entity.vo.WsExamPlan;
public class ArchiveClient {
public static void main(String args[]) throws Exception {
try {
String endpoint = "http://localhost:8080/exam/services/ExamService";
Service service = new Service();
Call call = (Call) service.createCall();
QName searchresultqn = new QName("urn:BeanService",
"ExamPlanContainer");
Class searchresultcls = ExamPlanContainer.class;
call
.registerTypeMapping(searchresultcls, searchresultqn,
new BeanSerializerFactory(searchresultcls,
searchresultqn),
new BeanDeserializerFactory(searchresultcls,
searchresultqn));
QName wsExamPlanQN = new QName("urn:BeanService", "WsExamPlan");
Class wsepcls = WsExamPlan.class;
call.registerTypeMapping(wsepcls, wsExamPlanQN,
new BeanSerializerFactory(wsepcls, wsExamPlanQN),
new BeanDeserializerFactory(wsepcls, wsExamPlanQN));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("ExamService",
"getAllowAnswerExamPlanContainer"));
call.addParameter("userId", XMLType.XSD_LONG, ParameterMode.IN);
call.addParameter("offset", XMLType.XSD_LONG, ParameterMode.IN);
call.addParameter("maxRow", XMLType.XSD_LONG, ParameterMode.IN);
call.setReturnType(searchresultqn);
ExamPlanContainer result = (ExamPlanContainer) call
.invoke(new Object[] { new Long(1), new Long(0),
new Long(1000) });
if (result != null) {
List list = result.getExamPlanList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
WsExamPlan wsExamPlan = (WsExamPlan) iter.next();
System.out.println(wsExamPlan.getName());
}
}
} catch (javax.xml.rpc.ServiceException e) {
e.printStackTrace();
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
} catch (java.rmi.RemoteException e) {
e.printStackTrace();
}
}
public static void testCount() {
try {
String endpoint = "http://localhost:8080/exam/services/ExamService";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("ExamService",
"getAllowAnswerExamPlanCount"));
call.addParameter("userId", XMLType.XSD_LONG, ParameterMode.IN);
call.setReturnType(XMLType.XSD_INT);
Integer result = (Integer) call
.invoke(new Object[] { new Long(1) });
System.out.println(result);
} catch (javax.xml.rpc.ServiceException e) {
e.printStackTrace();
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
} catch (java.rmi.RemoteException e) {
e.printStackTrace();
}
}
}
第五步:get方式访问测试
http://localhost:8080/exam/services/ExamService?method=getAllowAnswerExamPlanContainer¶mter0=1¶mter1=1¶mter2=1