微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService CXF and Spring2.5

@H_502_0@
@H_502_0@ 以下文章是教如何使用CXF与Spring2.5结合 
WEB.XML的配置 
Java代码  

收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <!--spring configuration -->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>/WEB-INF/appContext.xml</param-value>  
  12.     </context-param>  
  13.     <listener>  
  14.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.     </listener>  
  16.     <!-- xcf configuration -->  
  17.     <servlet>  
  18.         <servlet-name>CXFServlet</servlet-name>  
  19.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-     </servlet>  
  20.     <servlet-mapping>  
  21.         <url-pattern>/*</url-pattern>  
  22.     </servlet-mapping>  
  23.   <welcome-file-list>  
  24.     <welcome-file>index.jsp</welcome-file>  
  25.   </welcome-file-list>  
  26. </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/beans/spring-beans-2.5.xsd  
  • //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://incubator.apache.org/cxf/faq.html regarding CXF + Spring AOP -->  
  •     <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接口 
    //pizza  
  • 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""[email protected]",  
  •                 "Great job"));  
  • new Message("Dardy""Chen""[email protected]",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:\\newla\\testWebServiceClien\\Webroot\\WEB-INF\\appContext.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.  

  • 问题:为什么加了配置还是转换类型失败.上网查了很久都没答案.请大家会的也帮一下. 
    @H_502_0@

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐