如果还没有接触过axis,可以先看入门文章
axis1-myeclipse/eclipse-入门级别-资源、流程、常见问题
0、首先在myeclipse中新建一个web项目 webserviceAxis1
1、为项目 webserviceAxis1 引入 axis1 必须的几个jar包
2、创建实体类 TheUser.java 注意需要序列化即实现接口Serializable,这对于在webservice 中传递实体类型是必须的
TheUser.java 的内容如下
- package com.demo.model;
-
- import java.io.Serializable;
-
- public class TheUser implements Serializable {
-
-
-
-
- private static final long serialVersionUID = -971720598087640397L;
-
- private String username;
- private int age;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
-
- return "username="+username+" ;age="+age;
- }
-
- }
3、然后是编写webservice要发布的方法所在的类 TheUserWsddAction.java
TheUserWsddAction.java 的代码如下:
4、为了把webservice服务发布出去,我们需要修改项目的web.xml 的内容,具体参考
http://blog.csdn.net/bestcxx/article/details/52300248 中的
8、在自己的项目中使用WSDD方式发布webservice服务,如其所言
如果你还记得axis 这个项目,把他的web.xml文件找出来,复制里面的内容,替换掉helloworld的web.xml (头部可以保留编码格式,内部的AdminServlet可以注释掉,尽管没有啥影响)
这里我把我的项目的文件贴出来,如果你看的看上面的文章,可以直接复制粘贴到你的web.xml文件中
- xml version="1.0" encoding="ISO-8859-1"?>
-
-
- Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
-
- <web-app>
- <display-name>Apache-Axisdisplay-name>
-
- <listener>
- <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListenerlistener-class>
- listener>
-
- <servlet>
- <servlet-name>AxisServletservlet-name>
- <display-name>Apache-Axis Servletdisplay-name>
- <servlet-class>
- org.apache.axis.transport.http.AxisServlet
- servlet-class>
- servlet>
-
-
-
- <servlet>
- <servlet-name>SOAPMonitorServiceservlet-name>
- <display-name>SOAPMonitorServicedisplay-name>
- <servlet-class>
- org.apache.axis.monitor.SOAPMonitorService
- servlet-class>
- <init-param>
- <param-name>SOAPMonitorPortparam-name>
- <param-value>5001param-value>
- init-param>
- <load-on-startup>100load-on-startup>
- servlet>
-
- <servlet-mapping>
- <servlet-name>AxisServletservlet-name>
- <url-pattern>/servlet/AxisServleturl-pattern>
- servlet-mapping>
-
- <servlet-mapping>
- <servlet-name>AxisServletservlet-name>
- <url-pattern>*.jwsurl-pattern>
- servlet-mapping>
-
- <servlet-mapping>
- <servlet-name>AxisServletservlet-name>
- <url-pattern>/services/*url-pattern>
- servlet-mapping>
-
- <servlet-mapping>
- <servlet-name>SOAPMonitorServiceservlet-name>
- <url-pattern>/SOAPMonitorurl-pattern>
- servlet-mapping>
-
-
-
-
-
-
- <session-config>
-
- <session-timeout>5session-timeout>
- session-config>
-
-
- <parameter name="allowedMethods" value="getName" />
- <parameter name="scope" value="request" />
- service>
- <service name="TheUserWsddAction" provider="java:RPC">
- <parameter name="className" value="com.demo.action.TheUserWsddAction" />
-
- <parameter name="allowedMethods" value="*" />
- <parameter name="scope" value="request" />
-
- <beanMapping qname="myNSD:TheUser" xmlns:myNSD="urn:TheUser" languageSpecificType="java:com.demo.model.TheUser"/>
- service>
- deployment>
undeploy.wsdd
- <undeployment xmlns="http://xml.apache.org/axis/wsdd/">
- <service name="HelloWorldWSDD"/>
- <service name="TheUserWsddAction"/>
- undeployment>
6、启动服务,为了更为真实的实现那种效果,我把目前的项目打了war包,丢到了本地的tomcat服务器
然后在DOS环境中执行wsdd服务加载的命令-注意项目名和端口号
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://localhost:8080/webserviceAxis1/servlet/AxisServlet deployall.wsdd
然后访问一下,看发布是否成功
http://localhost:8080/webserviceAxis1/services
如下就成功了
7.生成客户端代码,还是再DOS环境下运行
D:\tomcat7\webapps\webserviceAxis1\WEB-INF>Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/webserviceAxis1/services/TheUserWsddAction?wsdl
8、将客户端代码复制到另外的一个项目中,同时需要引入那8个jar包
你会发现,相较而言,多了一个TheUser.java 当然这个类的位置你是可以自己决定的
因为本次发布的webservice方法含有复杂的返回类型
然后对代码做一点更改,即报错的地方有两种
一个是 TheUser_pkg.TheUser 改为TheUser
另一个是org.apache.xml.xml_soap.MapItem.class;改为 Map.class
9.最后是测试类
TheAxis1Test.java
- import java.rmi.RemoteException;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
-
- import javax.xml.namespace.QName;
- import javax.xml.rpc.ParameterMode;
- import javax.xml.rpc.ServiceException;
-
- import localhost.webserviceAxis1.services.TheUserWsddAction.TheUser;
- import localhost.webserviceAxis1.services.TheUserWsddAction.TheUserWsddActionService;
- import localhost.webserviceAxis1.services.TheUserWsddAction.TheUserWsddActionServiceLocator;
-
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.apache.axis.encoding.XMLType;
- import org.apache.axis.encoding.ser.BeanDeserializerFactory;
- import org.apache.axis.encoding.ser.BeanSerializerFactory;
- import org.junit.Ignore;
- import org.junit.Test;
-
- public class TheAxis1Test {
-
- private static final String url="http://localhost:8080/webserviceAxis1/services/TheUserWsddAction";
-
-
-
-
-
-
- @Test
-
- public void testgetEasyEg() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
-
-
-
-
-
-
-
- call.setOperationName(new QName(null,"getEasyEg"));
-
-
- String result=(String)call.invoke(new Object[]{"Jecket"});
- System.out.println(result);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @Test
-
- public void testgetTheUser() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
- QName qn = new QName("urn:TheUser", "TheUser");
-
-
- call.registerTypeMapping(TheUser.class, qn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
-
-
- call.setOperationName("getTheUser");
-
-
-
-
-
-
- call.addParameter("username", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter("age", XMLType.XSD_INTEGER, ParameterMode.IN);
- call.setReturnClass(TheUser.class);
- TheUser tuser = (TheUser) call.invoke(new Object[] {"Jecket",20});
- System.out.println("你获取了tuser ,名字是:"+tuser.getUsername()+" 年龄是 "+ tuser.getAge()+" 其他信息 "+tuser.getClass());
-
-
- }
-
-
-
-
-
-
-
-
- @Test
-
- public void testgetTheUserList() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
- QName qn = new QName("urn:TheUser", "TheUser");
-
-
- call.registerTypeMapping(TheUser.class, qn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
-
-
- call.setOperationName("getTheUserList");
-
-
-
-
-
-
-
- call.addParameter("username", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter("age", XMLType.XSD_INTEGER, ParameterMode.IN);
-
-
- call.setReturnClass(List.class);
- List tus = (List) call.invoke(new Object[] {"Jecket",20});
-
- for(TheUser tu: tus){
- System.out.println("调用 getTheUserList() 你获取了tu ,名字是:"+tu.getUsername()+" 年龄是 "+ tu.getAge()+" 其他信息 "+tu.getClass());
- }
-
- }
-
-
-
-
-
-
-
-
-
-
- @Test
-
- public void testgetTheUserGroup() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
- QName qn = new QName("urn:TheUser", "TheUser");
-
-
- call.registerTypeMapping(TheUser.class, qn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
-
-
- call.setOperationName("getTheUserGroup");
-
-
-
-
-
-
- call.addParameter("username", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter("age", XMLType.XSD_INTEGER, ParameterMode.IN);
-
-
- call.setReturnClass(TheUser[].class);
-
- TheUser[] tu=(TheUser[]) call.invoke(new Object[] {"Jecket",20});
-
- for(TheUser tus:tu){
- System.out.println("调用了getTheUserGroup() 方法 ,年龄:"+tus.getAge()+" 姓名:"+tus.getUsername()+" 所属的类:"+tus.getClass());
- }
-
-
-
-
-
-
- }
-
- @Test
-
- public void testgetTheUserMap() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
- QName qn = new QName("urn:TheUser", "TheUser");
-
-
- call.registerTypeMapping(TheUser.class, qn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
-
-
- call.setOperationName("getTheUserMap");
-
-
- call.setReturnClass(Map.class);
-
- Map maps = (Map) call.invoke(new Object[]{});
-
-
-
-
-
-
-
-
- Iterator it=maps.keySet().iterator();
- while(it.hasNext()){
- TheUser ta=maps.get(it.next());
- System.out.println("调用了getTheUserMap() 方法 ,年龄:"+ta.getAge()+" 姓名:"+ta.getUsername()+" 所属的类:"+ta.getClass());
- }
-
- }
-
-
-
-
-
-
-
- @Test
-
- public void testhere() throws ServiceException, RemoteException{
- TheUserWsddActionService tuwas=new TheUserWsddActionServiceLocator();
- localhost.webserviceAxis1.services.TheUserWsddAction.TheUserWsddAction tuwa=tuwas.getTheUserWsddAction();
- String r=tuwa.getEasyEg("nihao");
- System.out.println(r);
- Object[] a=tuwa.getTheUserList("Jecket", 10);
- for(Object ob:a){
- TheUser ta=(TheUser)ob;
- System.out.println("年龄:"+ta.getAge()+" 姓名:"+ta.getUsername()+" 所属的类:"+ta.getClass());
- }
- }
-
-
- }
10、以上的例子入参都是简单的参数,比如String、int,如果是实体类、list等呢?
本例子为后续补充,不再赘述发布过程,只写出方法和调用方法
被调用的方法代码:
-
-
-
-
-
- public TheUser getByTheUser(TheUser theUser){
- return theUser;
- }
-
-
-
-
-
- public TheUser getByListTheUser(List list){
- return list.get(0);
- }
-
-
-
-
-
- public TheUser getByMapTheUser(Map map){
- return map.get("theUser0");
- }
调用webservice方法的代码
-
-
-
-
-
- @Test
-
- public void testGetByTheUser() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
- QName qn = new QName("urn:TheUser", "TheUser");
-
-
- call.registerTypeMapping(TheUser.class, qn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
-
-
- call.setOperationName("getByTheUser");
-
- call.addParameter("TheUser", XMLType.XSD_ANYTYPE, ParameterMode.IN);
-
- call.setReturnClass(TheUser.class);
-
- TheUser theUser=new TheUser();
- theUser.setAge(10);
- theUser.setUsername("jecket");
-
- TheUser theUser2=new TheUser();
- theUser2 = (TheUser)call.invoke(new Object[]{theUser});
- System.out.println("theUser2.name="+theUser2.getUsername());
-
-
- }
-
-
-
-
-
-
- @Test
-
- public void testGetByListTheUser() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
- QName qn = new QName("urn:TheUser", "TheUser");
-
-
- call.registerTypeMapping(TheUser.class, qn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
-
-
- call.setOperationName("getByListTheUser");
-
- call.addParameter("list", XMLType.XSD_ANYTYPE, ParameterMode.IN);
-
- call.setReturnClass(TheUser.class);
-
- TheUser theUser=new TheUser();
- theUser.setAge(10);
- theUser.setUsername("jecket");
- List list=new ArrayList();
- list.add(theUser);
- TheUser theUser2=new TheUser();
- theUser2 = (TheUser)call.invoke(new Object[]{list});
- System.out.println("theUser2.name="+theUser2.getUsername());
-
-
- }
-
-
-
-
-
-
- @Test
-
- public void testGetByMapTheUser() throws ServiceException, RemoteException{
-
- Service service=new Service();
-
-
- Call call=(Call) service.createCall();
-
-
- call.setTargetEndpointAddress(url);
-
-
- QName qn = new QName("urn:TheUser", "TheUser");
-
-
- call.registerTypeMapping(TheUser.class, qn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
-
-
- call.setOperationName("getByMapTheUser");
-
- call.addParameter("map", XMLType.XSD_ANYTYPE, ParameterMode.IN);
-
- call.setReturnClass(TheUser.class);
-
- TheUser theUser=new TheUser();
- theUser.setAge(10);
- theUser.setUsername("jecket");
- Map map=new HashMap();
- map.put("theUser0", theUser);
- TheUser theUser2=new TheUser();
- theUser2 = (TheUser)call.invoke(new Object[]{map});
- System.out.println("theUser2.name="+theUser2.getUsername());
-
-
- }
特别需要强调的是,在以实体类作为入参或者反参的时候,需要将建立的实体类序列化,即实体类是这样的,implements Serializable
- import java.io.Serializable;
-
- public class TheUser implements Serializable {
-
- /**
- * 提供序列化编号,远程调用时要用到
- */
- private static final long serialVersionUID = -971720598087640397L;
-
- private String username;
- private int age;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- return "username="+username+" ;age="+age;
- }
-
- }