1、创建一个动态的web项目,引入jar,
http://pan.baidu.com/s/1sk1vwDJ ,此网盘上有整个web项目,可以直接运行在tomcat5.5以上、jdk1.6 ,服务端配置:
1、xml配置引入:
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- WebService -->
<servlet>
<servlet-name>CXFServlet</servlet-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>/services/*</url-pattern>
</servlet-mapping>
2、在WEB-INF目录下新建一个xml,可以命名为applicationContext.xml ,也可以叫其他名字,但是需要在web.xml中引入,否则无法加载,(这里有客户端和服务端的配置,如果只需要服务端,客户端的配置可以删除掉)
如上面的web.xml代码,或者如下,如下的列子中,因为项目中有了applicationContext.xml 的文件,于是我改了个名字,叫webservice.xml,名字叫什么不重要,只要web.xml里引入即可
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:webservice.xml</param-value>
</context-param>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
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
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.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" />
<!-- Start 供WebService调用的业务Service -->
<bean id="test12333WS"
class="com.lbs.webservice.wsinterface.Test12333WSIMP">
</bean>
<jaxws:server id="test12333WSServer" address="/test12333WS">
<jaxws:serviceBean>
<ref bean="test12333WS" />
</jaxws:serviceBean>
</jaxws:server>
<!-- End 供WebService调用的业务Service -->
<!-- Start 客户端使用 -->
<bean id="client"
class="com.lbs.webservice.wsinterface.Test12333WS"
factory-bean="clientFactory"
factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.lbs.webservice.wsinterface.Test12333WS"/>
<property name="address" value="http://localhost:8080/clientws/services/test12333WS"/>
</bean>
<!-- End 客户端使用 -->
</beans>
3、服务端接口类(这里的服务端接口类,必须要写@WebService),如果是客户端的代码,那么必须要和服务端的包路径一样
import javax.jws.WebService;
/*注意:1、@WebService必须要写,否则会找不到接口方法、
* 2、接口Test12333WS放的路径,在什么包下要和服务端一模一样,否则也会报错
*
*
*
* */
@WebService
public interface Test12333WS {
public Test12333Dto say(String name);
}
4、服务端实现类(只有服务端才使用实现类,客户端不需要,只需要接口即可)
package com.lbs.webservice.wsinterface;
import javax.jws.WebService;
@WebService
public class Test12333WSIMP implements Test12333WS {
@Override
public Test12333Dto say(String name){
Test12333Dto dto = new Test12333Dto();
dto.setA(123456L);
dto.setB(name +"Hello World");
return dto;
}
}
5、参数传递实体类
package com.lbs.webservice.wsinterface;
public class Test12333Dto {
private java.lang.Long a;
private java.lang.String b;
}
----------------------------------------------------------服务端完成-------------------------------------------------------------------------
启动tomcat,输入地址http://127.0.0.1:8080/clientws/services/
即可以看到已经定义的webservice接口类和方法
------------------------------------------------------------客户端调用服务端------------------------------------------------------------------------------------------
方法1、直接写一个测试类,写一个main函数,函数代码如下:(
JaxWsProxyFactoryBean需要引入一个jar包,cxf2.2.jar,项目里面已经有了
)
/**
* 通过代理API调用,依赖于服务端的接口 // 调用WebService方式1、
*/
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(Test12333WS.class);
factory.setAddress("http://127.0.0.1:8080/clientws/services/test12333WS");
Test12333WS service = (Test12333WS) factory.create();
Test12333Dto dto = service.say("老何");
System.out.println(dto.getA());
}
方法2、配置,配置即在applicationContext.xml文件中不删除客户端代码,然后在jsp页面(这里只是列子,为了简单,所以就直接在index.jsp中写了java代码,代码如下:)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@page import="com.lbs.webservice.wsinterface.Test12333WS"%>
<%@page import="com.lbs.webservice.wsinterface.Test12333Dto"%>
<%
ApplicationContext factory = new ClassPathXmlApplicationContext("../applicationContext.xml");
Test12333WS client = (Test12333WS)factory.getBean("client");
Test12333Dto dto = client.say("老何");
System.out.println(dto.getAab002());
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
服务器启动没有问题;客户端调用出现问题。
抛出的异常为
警告: Failed to create application context.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cxf.wsdl.WSDLManager' defined in class path resource [META-INF/cxf/cxf.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.apache.cxf.wsdl11.WSDLManagerImpl]: Constructor threw exception;
解决方法:
修改项目的.classpath文件
找到
<classpathentry kind="con" path="melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_5_CONTAINER"/>
修改为
<classpathentry kind="con" path="com.genuitec.eclipse.j2eedt.core.J2EE14_CONTAINER "/>