第一步:建一个secn的web文件
第二步:建一个secn_client的java文件
第三步:在secn_client里那一个deploy.wsdd文件:
<!-- 创建描述部署文件
将deploy.wsdd发送到服务器上部署;生成 server-config.wsdd
-->
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<!-- WebServer服务类 -->
<service name="HelloWorldService" provider="java:RPC">
<!-- 操作的className参数对应的类名 -->
<parameter name="className" value="cn.itcast.axis.service.HelloWorldService" />
<!-- allowedMethods所能操作的方法;*为所有的方法,不能为private修饰符的 -->
<parameter name="allowedMethods" value="*" />
<parameter name="scope" value="application" />
</service>
</deployment>
第四步:要配置web.xml的axis信息:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Apache-Axis</display-name>
<listener>
<listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
</listener>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<display-name>Axis Admin Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AdminServlet
</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<display-name>SOAPMonitorService</display-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>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>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>
<session-config>
<!-- Default to 5 minute session timeouts -->
<session-timeout>5</session-timeout>
</session-config>
<!-- currently the W3C havent settled on a media type for WSDL; http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
for now we go with the basic 'it's XML' response -->
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<welcome-file-list id="WelcomeFileList">
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jws</welcome-file>
</welcome-file-list>
</web-app>
第五步:要生成server-config.wsdd文件
Main class:
org.apache.axis.client.AdminClient
A
rguments:
-l http://localhost:8000/L_axis/servlet/AxisServlet src/cn/itcast/axis/widget/deploy.wsdd
或
-l http://localhost:8000/L_axis/servlet/AxisServlet deploy.wsdd
在地址栏里输入:http://localhost:8080/secn/services/UserInfo?wsdl
下面显示集合形式
@Test
public void transaListEmployee() throws Exception {
Service service = new Service();
Call call = (Call) service.createCall();
String url = "http://dh.secn.com.cn/services/CollService";
call.setTargetEndpointAddress(new URL(url));
call.setOperationName("transListEmployee");
call.setMaintainSession(true);
Class clazz = Employee.class;
// 限定一定需要跟server-config.wsdd文档中配置限定一致,严格区分大小写
QName qname = new QName("urn:coll.webservice.secn.com.cn", "Employee");
call.registerTypeMapping(clazz, qname, new BeanSerializerFactory(clazz,
qname), new BeanDeserializerFactory(clazz, qname));
List<Employee> liste = new ArrayList<Employee>();
Employee c = new Employee();
c.setName("tom");
liste.add(c);
c = new Employee();
c.setName("tom1");
liste.add(c);
call.invoke(new Object[] { liste });
}
<!-- qname的ns可以随便写,但一定要和后台的ns相同;xmlns:ns后台的名字可以随便写,一般配写法是把包名倒过来写 -->
<beanMapping qname="ns:Employee" xmlns:ns="urn:coll.webservice.secn.com.cn"
languageSpecificType="java:cn.com.secn.webservice.coll.Employee"/>
第六步:WSDL2Java
Main class:
org.apache.axis.wsdl.WSDL2Java
Arguments:
-v -p cn.com.secn.webservice.axsis.client.coll -o src
http://dh.secn.com.cn/services/CollService?wsdl
调用方法:
//定位器的方法
CollServiceServiceLocator locator = new CollServiceServiceLocator();
//生成存根
CollService_PortType port = locator.getCollService();
//
String str = (String)port.transaList(new String[]{"jerry","jerry1"});
System.out.println(str);
第七步:给webService写日志文件
public class LogHandler extends BasicHandler {
private static final long serialVersionUID = 1L;
/**
* 调用
*/
public void invoke(MessageContext msgContext) throws AxisFault {
try {
Handler serviceHandler = msgContext.getService();
String filename = (String)getOption("filename");
//输出流
FileOutputStream fos = new FileOutputStream(filename, true);
PrintWriter writer = new PrintWriter(fos);
//访问次数
Integer numAccesses = (Integer)serviceHandler.getOption("accesses");
if (numAccesses == null){
numAccesses = new Integer(0);
}
numAccesses = new Integer(numAccesses.intValue() + 1);
//设置访问时间
Date date = new Date();
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd hh:mm:s");
String result = f.format(date) + ": service " + msgContext.getTargetService() + " accessed " + numAccesses + " time(s).";
serviceHandler.setOption("accesses", numAccesses);
writer.println(result);
writer.close();
} catch (Exception e) {
throw AxisFault.makeFault(e);
}
}
}