以下文章是教如何使用CXF与Spring2.5结合?
WEB.XML的配置?
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <web-app?version="2.5"???
- ????xmlns="http://java.sun.com/xml/ns/javaee"???
- ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
- ????xsi:schemaLocation="http:??
- ????http:??
- ??????
- ????<!--spring?configuration?-->??
- ????<context-param>??
- ????????<param-name>contextConfigLocation</param-name>??
- ????????<param-value>/WEB-INF/appContext.xml</param-value>??
- ????</context-param>??
- ????<listener>??
- ????????<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>??
- ????</listener>??
- ????<!--?xcf?configuration?-->??
- ????<servlet>??
- ????????<servlet-name>CXFServlet</servlet-name>??
- ????????<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet- ????</servlet>??
- ????<servlet-mapping>??
- ????????<url-pattern>/*</url-pattern>??
- ????</servlet-mapping>??
- ??<welcome-file-list>??
- ????<welcome-file>index.jsp</welcome-file>??
- ??</welcome-file-list>??
- </web-app>??
Spring文件的配置?
??
<beans?xmlns="http://www.springframework.org/schema/beans"??
????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
????xmlns:context="http://www.springframework.org/schema/context"??
????xmlns:cxf="http://cxf.apache.org/core"??
????xmlns:jaxws="http://cxf.apache.org/jaxws"??
//www.springframework.org/schema/beans??
????????http:??
//www.springframework.org/schema/context??
//www.springframework.org/schema/context/spring-context-2.5.xsd??
//cxf.apache.org/core??
//cxf.apache.org/schemas/core.xsd??
//cxf.apache.org/jaxws??
//cxf.apache.org/schemas/jaxws.xsd"??
????default-autowire="byName">??
????<!--?Load?CXF?modules?from?cxf.jar?-->??
????<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"?/>??
????<!--?Enable?message?logging?using?the?CXF?logging?feature?-->??
????<cxf:bus>??
????????<cxf:features>??
????????????<cxf:logging/>??
????????</cxf:features>??
????</cxf:bus>??
????<!--?The?service?bean?-->??
????<bean?id="contactUsServiceImpl"?class="com.webservice.serviceImpl.ContactUsServiceImpl"/>??
????<!--?Aegis?data?binding?-->??
????<bean?id="aegisBean"??
????????class="org.apache.cxf.aegis.databinding.AegisDatabinding"??
????????scope="prototype"/>???
????<bean?id="jaxws-and-aegis-service-factory"??
class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"??
????????scope="prototype">??
????????<property?name="dataBinding"?ref="aegisBean"/>??
????????<property?name="serviceConfigurations">??
????????????<list>??
??????????????<bean?class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"/>??
class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"/>??
class="org.apache.cxf.service.factory.DefaultServiceConfiguration"/>???
????????????</list>??
????????</property>??
????</bean>??
???
????<!--?Service?endpoint?-->??
????<!--?See?http:??
????<jaxws:endpoint?id="contactUsService"??
????????????implementorClass="com.webservice.serviceImpl.ContactUsServiceImpl"??
????????????implementor="#contactUsServiceImpl"??
????????????address="/contactus">??
????????<jaxws:serviceFactory>??
????????????<ref?bean="jaxws-and-aegis-service-factory"/>??
????????</jaxws:serviceFactory>??
????</jaxws:endpoint>??
</beans>??
建立service接口?
??
package?com.webservice.service;??
import?java.util.*;??
import?com.webservice.pojo.*;??
import?javax.jws.WebParam;??
import?javax.jws.WebService;??
@WebService??
public?interface?ContactUsService?{??
????List<Message>?getMessages();??
????String?getUserName();??
void?postMessage(@WebParam(name?=?"message")?Message?message);??
}??
实现接口?
package?com.webservice.serviceImpl;??
import?com.webservice.pojo.Message;??
import?com.webservice.service.ContactUsService;??
@WebService(endpointInterface?=?"com.webservice.service.ContactUsService")??
class?ContactUsServiceImpl?implements?ContactUsService?{??
????@Override??
public?List<Message>?getMessages()?{??
????????List<Message>?messages?=?new?ArrayList<Message>();??
????????messages.add(new?Message("Willie",?"Wheeler",?"willie.wheeler@xyz.com",??
????????????????"Great?job"));??
new?Message("Dardy",?"Chen",?"dardy.chen@xyz.com",250); line-height:18px"> ????????????????"I?want?my?money?back"));??
return?messages;??
????}??
void?postMessage(Message?message)?{??
????????System.out.println(message);??
public?String?getUserName()?{??
??????????
return?"nofeng";??
接口中用到的javaBean(实体bean)?
package?com.webservice.pojo;??
import?java.io.Serializable;??
class?Message?implements?Serializable{??
private?static?final?long?serialVersionUID?=?6452500932154823754L;??
private?String?firstName;??
private?String?lastName;??
private?String?email;??
private?String?text;??
public?Message(String?firstName,String?lastName,String?email,String?text){??
this.firstName=firstName;??
this.lastName=lastName;??
this.email=email;??
this.text=text;???????
public?String?getFirstName()?{??
return?firstName;??
void?setFirstName(String?firstName)?{??
this.firstName?=?firstName;??
public?String?getLastName()?{??
return?lastName;??
void?setLastName(String?lastName)?{??
this.lastName?=?lastName;??
public?String?getEmail()?{??
return?email;??
void?setEmail(String?email)?{??
this.email?=?email;??
public?String?getText()?{??
return?text;??
void?setText(String?text)?{??
this.text?=?text;??
以上是服务端.我们可以通过Tomcat来发布.然后查看?
http://localhost:8888/testWebService/contactus?wsdl?
==================================================================?
以下是客户端的实现?
在接口的类中需要添加以下配置.因为接口中的getMessages方法是返回一个自定义类型List.该配置文件名要规定为ContactUsService.aegis.xml.?
ContactUsService是你的接口名?
<mappings>??
????<mapping>??
????????<method?name="getMessages">??
????????????<return-type?componentType="com.webservice.pojo.Message"?/>??
????????</method>??
????</mapping>??
</mappings>??
package?com.webservice.test;??
import?org.springframework.context.ApplicationContext;??
import?com.webservice.service.*;??
class?TestWebServiceClient?{??
void?main(String[]?args)?{??
????????String?SPRING_CONFIG="D:newlatestWebServiceClienWebRootWEB-INFappContext.xml";??
????????ApplicationContext?context;??
????????ContactUsService?contactUsService;??
????????context=new?org.springframework.context.support.FileSystemXmlApplicationContext(SPRING_CONFIG);??
????????contactUsService=(ContactUsService)?context.getBean("contactUsService");??????????
????????System.out.println(contactUsService.getUserName());??
????????System.out.println(contactUsService.getMessages().size());??
运行结果?
nofeng??
2008-5-27?17:40:07?org.apache.cxf.phase.PhaseInterceptorChain?doIntercept??
信息:?Interceptor?has?thrown?exception,?unwinding?now??
org.apache.cxf.interceptor.Fault:?Couldn't?instantiate?class.?com.webservice.pojo.Message.??
问题:为什么加了配置还是转换类型失败.上网查了很久都没答案.请大家会的也帮一下.?
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|