一。程序结构
二。所需要的包:
三。编辑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">
PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN"
" http://java.sun.com/dtd/web-app_2_3.dtd">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</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-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>
四。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">
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>
<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
);
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;
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 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;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
public class MyObject
{
private int id;
private String name;
{
private int id;
private String name;
public int getId()
{
return id;
}
{
return id;
}
public void setId(int id)
{
this.id = id;
}
{
this.id = id;
}
public String getName()
{
return name;
}
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
{
this.name = name;
}
}
。。。。。。~~OK~~服务端搞定,用tomcat启动webserviceTest。
可以看到
就说明服务端对了。
现在可以用客户端来测试:
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
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"),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(); } } }
{
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"),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(); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。