我正在使用Axis API访问Axis HTTP服务器.
可以在 here找到API的文档.
可以在 here找到API的文档.
我使用以下代码将处理程序添加到服务器. service的类型为java.xml.rpc.Service
HandlerRegistry registry = service.getHandlerRegistry(); QName serviceName = new QName(url,"MyServiceClass"); List<HandlerInfo> handlerChain = new ArrayList<HandlerInfo>(); HandlerInfo handlerInfo = new HandlerInfo(MyHandler.class,null,null); handlerChain.add(handlerInfo); registry.setHandlerChain(serviceName,handlerChain);
我知道服务名称是正确的,因为我在后续调用服务对象时获得了正确的输出.
不知何故,处理程序没有被调用.这是Handler类.
我的目的是在将请求转发给服务器之前向HTTP请求添加自定义标头.
import javax.xml.namespace.QName; import org.apache.axis.AxisFault; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; public class MyHandler extends BasicHandler { @Override public void init() { System.out.println("init called"); super.init(); System.out.println("init called"); } @Override public void cleanup() { super.cleanup(); System.out.println("cleanup called"); } @Override public void invoke(MessageContext mc) throws AxisFault { System.out.println("invoke called"); } public QName[] getHeaders() { System.out.println("getHeaders"); return new QName[1]; } }
上面的代码有什么问题?
解决方法
Okie.这应该做的伎俩:
1 – 创建一个包含以下内容的wsdd文件(例如/tmp/test.wsdd):
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <handler name="test" type="java:axistest.TestHandler" /> <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"> <requestFlow> <handler type="test"/> </requestFlow> </transport> </deployment>
2 – 确保所有轴库都在您的类路径中,然后运行:
java org.apache.axis.utils.Admin client /tmp/test.wsdd
3 – 步骤2将生成client-config.wsdd.将其复制到项目中,并确保在项目运行时它将位于类路径中.
4 – 所有Web服务调用(通过Http传输)将通过TestHandler1类进行路由
这是我的TestHandler1类(稍微修改了访问MIME头的ur处理程序):
package axistest; import javax.xml.namespace.QName; import javax.xml.soap.MimeHeaders; import org.apache.axis.AxisFault; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; public class TestHandler1 extends BasicHandler { @Override public void init() { System.out.println("init called"); super.init(); System.out.println("init called"); } @Override public void cleanup() { super.cleanup(); System.out.println("cleanup called"); } @Override public void invoke(MessageContext mc) throws AxisFault { System.out.println("invoke called"); System.out.println("=----------------------------------="); MimeHeaders mimeHeaders = mc.getMessage().getMimeHeaders(); mimeHeaders.addHeader("X-Test","Hello"); System.out.println("Headers : \n " + mimeHeaders); } public QName[] getHeaders() { System.out.println("getHeaders"); return new QName[1]; } }
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled. init called init called invoke called =----------------------------------= Headers : org.apache.axis.message.MimeHeaders@761eec35 . . .