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.bat
2、生成代码的使用
/*
* Created on 2004-1-17
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package localhost.axis.pdw_jws;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
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 -->
<handler name="logHandler" type="java:webservices.handlers.LogHandler">
<parameter name="filename" value="c:\\axislog.txt"/>
</handler>
<!-- 认证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.*;
/**
* WebService 服务类(测试省略了接口)
* @author peidw
*
*/
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"));
}
/**
* 返回对象类型
* @param isbn
* @return
*/
public Book getBookByIsbn(String isbn){
Book result=null;
if(map.containsKey(isbn)){
return (Book) map.get(isbn);
}
return result;
}
/**
* 返回一个list类型
* @return
*/
public List getBookList(){
List result=null;
Set set=map.entrySet();
result=new ArrayList();
result.addAll(set);
return result;
}
/**
* 返回对象数组
* @return
*/
public Book[] getBookArray(){
List tmp_list=getBookList();
return (Book[]) tmp_list.toArray();
}
/**
* 返回一个Map
* @return
*/
public Map getAllBookMap(){
return map;
}
/**
* 返回对象测试
* @return
*/
public Student getStudent(){
return new Student("小裴","kkk@tom.com","广西合浦西场裴屋村");
}
}
两个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.*;
/**
* 日志handler ,handler在axis的地位,相当于servlet的filter!可以在所有的service中添加多个handler
* @author KT4600
*
*/
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) {
// TODO Auto-generated catch block
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;
/**
* 自己写的认证handler,也可以使用axis自带的,只需配置在server-config.wsdd中添加
* <parameter name="allowedRoles" value="ronghao"/> 证证规则即可
* @author peidw
*
*/
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;
/**
* axis1.x客户端调用测试
* @author peidw
*
*/
public class MyServiceExe {
/**
* 使用axis的WSDL2JAVA调用WS服务,如果服务器端添加了简单handler用户验证,则需要修改
* ..SoapBindingStub类的createCall()方法,
* @throws Exception
*/
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);
}
}
/**
* 通过动态接口方式调用,使用axis提供的client包
* 最简单的使用方式
* @throws Exception
*/
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);
}
/**
* 使用WSIF来调用axis发布的服务
* <pre>
* WSIF具有可对任何 Web 服务进行独立于绑定的访问的 API。
* 它紧密依赖于 WSDL,因此可以调用可在 WSDL 中描述的任何服务。
* </pre>
* @throws Exception
*/
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();
}
}
posted on 2007-10-25 16:25
有猫相伴的日子 阅读(3525)
评论(0) 编辑 收藏 所属分类:
webservices