一个简单的使用CXF编写基于spring的webservice

  

服务端:

创建实体类

package com.qdwl.ws.entity;

public class Order {  

  private int id;   

  private String name;  

  private double price;    

  public Order() {   

  super();  

  }  

  public Order(int id2, String string, int i) {

    // TODO Auto-generated constructor stub  

  }  

  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;  

  }  

  public double getPrice() {   

  return price;  

  }  

  public void setPrice(double price) {   

  this.price = price;  

  }

} 

 

 

创建SEI接口  

package com.qdwl.ws;

import javax.jws.WebMethod; import javax.jws.WebService;

import com.qdwl.ws.entity.Order;

@WebService

public interface OrderWs {  

  @WebMethod  

  public Order getOrderById(int id);

}

 

创建SEI实现类:

package com.qdwl.ws;

import javax.jws.WebService;

import com.qdwl.ws.entity.Order;

@WebService

public class OrderWsImpl implements OrderWs {  

  //此处使用构造方法为了确认容器创建该实现的时间。

  public OrderWsImpl(){  

   System.out.println("1111111");  

  }  

  @Override  

  public Order getOrderById(int id) {

    System.out.println("server getOrderById()"+id);  

     return new Order(24,"飞机",10000000);  

  }  

}

 

在src中创建bean.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- <beans  xmlns="http://www.springframework.org/schema/beans"    

xmlns:context="http://www.springframework.org/schema/context"    

xmlns:jaxws="http://cxf.apache.org/jaxws"   

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    

xsi:schemaLocation="http://www.springframework.org/schema/beans >    

http://www.springframework.org/schema/beans/spring-beans.xsd    

http://www.springframework.org/schema/context    

http://www.springframework.org/schema/context/spring-context.xsd    

http://cxf.apache.org/jaxws    

http://cxf.apache.org/schemas/jaxws.xsd">   

</beans> -->

 

博主刚开始使用上面注释掉的约束结果服务器启动时会报如下错误后改用下面的约束就好了

nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 5; cvc-elt.1: Cannot find the declaration of element 'beans'

<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:p="http://www.springframework.org/schema/p"         

  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://www.springframework.org/schema/context            

  http://www.springframework.org/schema/context/spring-context.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"/>       

  <jaxws:endpoint id="orderProcess" 

   <!--SEI实现类完整类名-->     

  implementor="com.qdwl.ws.OrderWsImpl"

  <!--放在工程明后用于访问wsdl文件-->     

  address="/OrderProcess" />    

</beans>

 

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  

  <display-name>111</display-name>  

  <welcome-file-list>    

  <welcome-file>index.html</welcome-file>    

  <welcome-file>index.htm</welcome-file>    

  <welcome-file>index.jsp</welcome-file>    

  <welcome-file>default.html</welcome-file>    

  <welcome-file>default.htm</welcome-file>    

  <welcome-file>default.jsp</welcome-file>  

  </welcome-file-list>  

 

<!-- 设置Spring容器加载配置文件路径 -->

<context-param>    

  <param-name>contextConfigLocation</param-name>    

  <param-value>classpath:beans.xml</param-value>

</context-param>

<listener>    

   <listener-class>      org.springframework.web.context.ContextLoaderListener     </listener-class>

</listener>

<servlet>    

  <servlet-name>CXFService</servlet-name>    

  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    

   <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>    

  <servlet-name>CXFService</servlet-name>    

  <url-pattern>/*</url-pattern>

</servlet-mapping>

</web-app>

 

启动服务器自动发布成功

访问地址

http://localhost:8080/ws-test3/OrderProcess?wsdl

客户端

client-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:jaxws="http://cxf.apache.org/jaxws" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.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"/> 
   
 <jaxws:client id="orderWsClient"

<!--此处配置的是生成类中的接口-->
   serviceClass="com.qdwl.ws.OrderWs"

<!--此处是上面的访问地址去掉?及后面部分-->
   address="http://localhost:8080/ws-test3/OrderProcess"/> 
 </beans> 

 java 代码

package com.qdwl.ws.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qdwl.ws.Order; import com.qdwl.ws.OrderWs;

public class ClientTest {  

  public static void main(String[] args) {   

  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-bean.xml"});   

  OrderWs orderWs = (OrderWs) context.getBean("orderWsClient");   

  Order res = orderWs.getOrderById(24);   

   System.out.println(res);  

  }

}

 

 好了一个简单的使用CXF基于spring的webservice开发到这里就结束了。

posted @ 2017-04-12 15:28  一只蜗牛1  Views(216)  Comments(0)    收藏  举报