摘自:http://www.blogjava.net/pdw2009/archive/2007/10/25/155889.html
axis常见问题及解决办法参考这里
http://www.ibm.com/developerworks/cn/webservices/ws-axisfaq/
根据wsdl开成webservice的java客户 例子:
1、执行以下命令生成客户端代码
Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/Hello.jws?wsdl
该命令的参数格式
从最简单的开始 ,-uri 指定wsdl文件
> WSDL2Java -uri currencyConvert.wsdl
-d 使用不同的data binding方法
> WSDL2Java -uri currencyConvert.wsdl -d xmlbeans
-a 生成异步的方法
> WSDL2Java -uri currencyConvert.wsdl -a
-t 生成测试case
> WSDL2Java -uri currencyConvert.wsdl -t
稍微复杂一些的,-p可以指定生成的package,-o指定生成的路径,-ss生成服务端代码
wsdl2java -uri ../wsdl/currencyConvert.wsdl -o ../gen_src -ss -sd -g -p foo.bat2、生成代码的使用
package localhost.axis.pdw_jws;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class clientTest {
public static void main(String[] args) throws ServiceException, RemoteException {
PdwService service=new PdwServiceLocator();
Pdw p=service.getpdw();
int count=p.add(3,4);
System.out.println(count);
System.out.println(p.sub(4,6));
}
} 开发环境地的建立和以往一样,把例子中的classes的文件打包成jar,放到开发环境,就可能了。
这是我3年前写的,在myeclipse中进行axis开发的文件
http://www.54bk.com/user1/6324/archives/2005/22197.html
最近复习写的代码
server-config.xml 代码如下:
<?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="attachments.Directory" value="D:\resin-pro-3.1.0\webapps\axis\WEB-INF\attachments"/>
<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
<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="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<transport name="local">
<responseFlow>
<handler type="LocalResponder"/>
</responseFlow>
</transport>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
</transport>
<handler name="soapmonitor"
type="java:org.apache.axis.handlers.SOAPMonitorHandler">
<parameter name="wsdlURL"
value="/axis/SOAPMonitorService-impl.wsdl"/>
<parameter name="namespace"
value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/ >
<parameter name="serviceName" value="SOAPMonitorService"/>
<parameter name="portName" value="Demo"/>
</handler>
<handler name="logHandler" type="java:webservices.handlers.LogHandler">
<parameter name="filename" value="c:\\axislog.txt"/>
</handler>
<handler name="authorHandler" type="java:webservices.handlers.AuthenticationHandler"/>
<service name="SOAPMonitorService" provider="java:RPC">
<parameter name="allowedMethods" value="publishMessage"/>
<parameter name="className"
value="org.apache.axis.monitor.SOAPMonitorService"/>
<parameter name="scope" value="Application"/>
</service>
<service name="Version" provider="java:RPC">
<parameter name="allowedMethods" value="getVersion"/>
<parameter name="className" value="org.apache.axis.Version"/>
</service>
<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="MyServices" provider="java:RPC">
<parameter name="allowedMethods" value="*"/>
<parameter name="allowedRoles" value="peidw"/><!-- 验证规则 -->
<parameter name="className" value="webservices.MyServices"/>
<beanMapping languageSpecificType="java:domain.Book" qname="ns:Book" xmlns:ns="urn:BeanService" />
<beanMapping languageSpecificType="java:domain.Student" qname="ns:Student" xmlns:ns="urn:StudentService" />
<requestFlow>
<handler type="logHandler"/>
</requestFlow>
</service>
</deployment>myservice.java
package webservices;
import domain.Book;
import domain.Student;
import java.util.*;
public class MyServices {
static Map map=new HashMap();
static{
map.put("125-6922-10", new Book("Structs程序设计","孙卫琴","125-6922-10"));
map.put("125-6922-11", new Book("Think in Java","孙卫琴","125-6922-11"));
map.put("125-6922-12", new Book("C++程序高驻地","小张","125-6922-12"));
map.put("125-6912-59", new Book("CSS实践手册","小李","125-6912-59"));
map.put("125-6992-55", new Book("XML入门到精通","小裴","125-6992-55"));
} public Book getBookByIsbn(String isbn){
Book result=null;
if(map.containsKey(isbn)){
return (Book) map.get(isbn);
}
return result;
} public List getBookList(){
List result=null;
Set set=map.entrySet();
result=new ArrayList();
result.addAll(set);
return result;
} public Book[] getBookArray(){
List tmp_list=getBookList();
return (Book[]) tmp_list.toArray();
} public Map getAllBookMap(){
return map;
} public Student getStudent(){
return new Student("小裴","[email protected] "," 广西合浦西场裴屋村");
}
}两个handler
package webservices.handlers;
import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.commons.lang.*;
import java.io.*;
import java.util.*;
public class LogHandler extends BasicHandler{
public void invoke(MessageContext arg0) throws AxisFault {
Handler handler=arg0.getService();
String logfilename=(String)this.getOption("filename");
if(StringUtils.isEmpty(logfilename)){
throw new AxisFault("日志文件不能为空","",null,null);
}
try {
FileOutputStream fos=new FileOutputStream(logfilename,true);
PrintWriter pw=new PrintWriter(fos);
Date date=new Date();
arg0.getMessage().writeTo(System.out);
String result="---";
pw.println(result);
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package webservices.handlers;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.security.SecurityProvider;
import org.apache.axis.security.simple.SimpleSecurityProvider;
import org.apache.axis.session.Session;
public class AuthenticationHandler extends BasicHandler{
public void invoke(MessageContext arg0) throws AxisFault {
SecurityProvider provider = (SecurityProvider)arg0.getProperty("securityProvider");
if(provider==null){
provider= new SimpleSecurityProvider();
arg0.setProperty("securityProvider", provider); }
if(provider!=null){
String userId=arg0.getUsername();
String password=arg0.getPassword();
//对用户进行认证,如果authUser==null,表示没有通过认证,抛出Server.Unauthenticated异常。
Session session=arg0.getSession();
System.out.println("----------===xx----------------");
org.apache.axis.security.AuthenticatedUser authUser
= provider.authenticate(arg0);
if(authUser==null)
throw new AxisFault("Server.Unauthenticated","用户验证异常", null,null);
//用户通过认证,把用户的设置成认证了的用户。
arg0.setProperty("authenticatedUser", authUser);
}
}
}
客户端例子
package test;
import wsclient.myservices.*;
import java.net.URL;
import java.util.*;
import javax.xml.namespace.QName;
import org.apache.axis.client.*;
import org.apache.wsif.*;
import org.apache.wsif.wsdl.AuthenticatingProxyWSDLLocatorImpl;
public class MyServiceExe {
public static void handlerCallDemo()throws Exception{
MyServicesService ms=new MyServicesServiceLocator();
MyServices_PortType msp=ms.getMyServices();
Book book=msp.getBookByIsbn("125-6922-10");
System.out.println(book.getAuthor()+"-"+book.getBookname()+"-"+book.getIsbn());
Map map=msp.getAllBookMap();
Set keyset=map.keySet();
Iterator it=keyset.iterator();
String tmp=null;
while(it.hasNext()){
tmp=(String)it.next();
System.out.println(tmp);
}
}
public static void dynamicCall()throws Exception{
String endpoint_1="http://localhost:8000/axis/services/Version?wsdl ";
Service service = new Service();
Call call=(Call)service.createCall();
call.setOperationName(new QName(endpoint_1,"getVersion "));
call.setTargetEndpointAddress(new URL(endpoint_1));
String result=(String)call.invoke(new Object[]{});
System.out.println("result="+result);
}
public static void wsifCall()throws Exception {
String url="http://localhost:8000/axis/services/Version?wsdl ";
WSIFServiceFactory factory=WSIFServiceFactory.newInstance();
//如果调用的方法需要进行用户/密码校验,需执行下面代码
//AuthenticatingProxyWSDLLocatorImpl awsli=new AuthenticatingProxyWSDLLocatorImpl(url,"gaolong1","19831001"); //验证连接
WSIFService service=factory.getService(url,"http://localhost:8000/axis/services/Version","VersionService","http://localhost:8000/axis/services/Version","Version ");
WSIFPort port = service.getPort();
WSIFOperation operation = port.createOperation("getVersion","getVersionRequest",null);//根据给定的操作名称参 数operationName,输入元素名称inputName,输出元素名称
WSIFMessage input = operation.createOutputMessage();//设置输入参数
WSIFMessage output = operation.createOutputMessage();//设置输出参数
WSIFMessage fault = operation.createFaultMessage(); //异常信息
operation.executeRequestResponseOperation(input,output,fault); //执行请求
System.out.println(output.getObjectPart("getVersionReturn"));
}
public static void main(String[] args)throws Exception{
handlerCallDemo();
dynamicCall();
wsifCall();
}
}