axis实现webService--服务端+客户端

一。程序结构

axis实现webService--服务端+客户端_第1张图片
 
 

二。所需要的包:


axis实现webService--服务端+客户端_第2张图片
 

三。编辑web.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <!--tomcat启动时要读的文件-->
       <param-value>WEB-INF/beans.xml</param-value>
    </context-param>
    <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>
    <servlet>
       <servlet-name>CXFServlet</servlet-name>
       <display-name>CXF Servlet</display-name>
       <servlet-class>
           org.apache.cxf.transport.servlet.CXFServlet
       </servlet-class>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>CXFServlet</servlet-name>
       <!--拦截地址--->
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
</web-app>


四。beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!-- 定义一个bean  -->
    <bean id="hello" class="demo.spring.impl.HelloWorldImpl" />
    <!--  发布webservice implementor="#hello" 这个就是指的第一行定义的那个-->
    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>
五。HelloWorldImpl.java/HelloWorld.java/MyObject.java
package demo.spring;
import javax.jws.WebParam;
import javax.jws.WebService;
import entity.MyObject;

@WebService
public interface HelloWorld
{
    String stringInt(
      @WebParam(name="text",targetNamespace="http://spring.demo/") String text,
      @WebParam(name="num",targetNamespace="http://spring.demo/") String num
    );

    MyObject[] aryMyObjects(
      @WebParam(name="myObjects",targetNamespace="http://spring.demo/") MyObject[] myObjects
    );
}

package demo.spring.impl;
import javax.jws.WebService;
import demo.spring.HelloWorld;
import entity.MyObject;
@WebService
public class HelloWorldImpl implements HelloWorld
{
 public String stringInt(String text, String num)
   {
  System.out.print("stringInt called");
  return text+" too, "+num;
   }
 
   public MyObject[] aryMyObjects(MyObject[] myObjects)
   {
    MyObject[] ary=new MyObject[myObjects.length];
    for(int i=0;i<ary.length;i++)
    {
     ary[i]=new MyObject();
     ary[i].setId(myObjects[i].getId()+i);
     ary[i].setName(myObjects[i].getName()+" "+i);
    }
    return ary;
   }
}
package entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyObject", propOrder = {
    "id",
    "name"
})
public class MyObject
{
    private int id;
    private String name;
    public int getId()
    {
     return id;
    }
    public void setId(int id)
    {
     this.id = id;
    }
    public String getName()
    {
     return name;
    }
    public void setName(String name)
    {
     this.name = name;
    }
}


。。。。。。~~OK~~服务端搞定,用tomcat启动webserviceTest。
在IE里面输入http://localhost:8080/service/HelloWorld?wsdl
可以看到


axis实现webService--服务端+客户端_第3张图片
 

就说明服务端对了。
现在可以用客户端来测试:
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class ClientTest
{
 public static void main(String[] args)
 {
  testClient();
 }
 
 public static void testClient()
 {
  try
  {
   Service service = new Service();
   Call call = (Call) service.createCall();
   call.setTargetEndpointAddress("http://localhost:8080/service/HelloWorld?wsdl");
   //stringInt:服务端的方法名
   call.setOperationName(new QName("http://spring.demo/","stringInt"));
   //传值参数的定义
   call.addParameter(new QName("http://spring.demo/","text"), XMLType.XSD_STRING, ParameterMode.IN);
   call.addParameter(new QName("http://spring.demo/","num"), XMLType.XSD_STRING, ParameterMode.IN);
   call.setUseSOAPAction(true);
   //返回参数的类型
   call.setReturnType(XMLType.XSD_STRING);
   System.out.println(call.invoke(new Object[] {"i like you","forever"}));
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
}

你可能感兴趣的:(apache,spring,xml,webservice,servlet)