axis1 服务端配置
1、首先建立一个项目 axisTest 不需多说
2、在lib下放入需要的jar包 点击下载 :axis所需的jar包下载
3、然后需要在web.xml里面加入:
<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-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>
4、创建服务端的代码:
public class myService { public String getusername(String name){/**用spring的时候需要添加你要注入的bean baseTransaction = (BaseTransaction) getApplicationContext().getBean("baseTransaction")**/ return "Hello "+name+",this is an Axis Web Service"; } }
5、在WEB-INF下创建service-config.wsdd 文件,内容如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/> <service name="myService" provider="java:RPC"> <!--这里的name是你的service名字 访问的时候要用得着的--> <parameter name="className" value="com.service.myService"/> <!--这里的value是你所提供的的供外部访问的方法所在的类--> <parameter name="allowedMethods" value="getusername"/> <!--供外部访问的方法--> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> </requestFlow> </transport> </deployment>
6、启动tomcat 访问地址:http://localhost:8080/axisTest/servlet/AxisServlet
axis1 客户端调用
import java.net.MalformedURLException; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class myServiceTestorByWSDD { public static void main(String[] args) throws ServiceException,MalformedURLException, RemoteException { /**假如访问的时候有spring注入的情况 则需要 添加 baseTransaction = (BaseTransaction) getApplicationContext().getBean("baseTransaction"); 来获取spring的注入信息。实在service层添加的。**/ String endpoint = "http://localhost:8080/axisTest/services/myService"; Service service = new Service(); // 创建一个Service实例,注意是必须的! Call call = (Call) service.createCall(); // 创建Call实例,也是必须的! call.setTargetEndpointAddress(new java.net.URL(endpoint));// 为Call设置服务的位置 call.setOperationName("getusername"); // 注意方法名与JavaBeanWS.java中一样!! String res = (String) call.invoke(new Object[] { "邹萍" }); // 返回String,传入参数 System.out.println(res); } }
时间: 2024-11-16 15:57:50