您的位置:首页 > 其它

使用CXF框架开发一个简单的WebService

2017-08-06 09:26 746 查看
 1.新建一个Web工程做服务端

   #代码

Order.java

package com.bean;

public class Order {
private int id;
private String name;
private double price;
public Order(int id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
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;
}
@Override
public String toString() {
return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";
}

}
   
OrderWS.java

package com.ws;

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

import com.bean.Order;
@WebService
public interface OrderWS {
@WebMethod
public Order getOrderById(int id);
}
OrderWSImpl.java

package com.ws;

import javax.jws.WebService;

import com.bean.Order;

@WebService
public class OrderWSImpl implements OrderWS {
public OrderWSImpl(){
System.out.println("OrderWSImpl()");
}
@Override
public Order getOrderById(int id) {
// TODO Auto-generated method stub
System.out.println("server getOrderById() "+id);
return new Order(id, "火车票", 20000);
}

}

#配置文件

beans.xml

<?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"
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"> 
<!-- 引入cxf的一些核心配置 -->
<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="orderWS"
implementor="com.ws.OrderWSImpl"
address="/orderws" />
</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>CXFWead</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>

<!-- 应用启动的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置beans.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>

<!-- 所有的请求都会经过CXF框架 -->
<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>/*</url-pattern>
</servlet-mapping>
</web-app>

2.新建一个Web工程做客户端

#配置CXF环境变量,输入CMD命令自动生成服务端代码



 #配置文件

client-beans.xml

<?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"
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"> 
<jaxws:client id="orderClient"
serviceClass="com.ws.OrderWS" address="http://localhost:8013/CXFWead/orderws"/>
</beans>

ClientTest.java

package com.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ws.Order;
import com.ws.OrderWS;

public class ClintTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
OrderWS orderWS = (OrderWS) context.getBean("orderClient");
Order order = orderWS.getOrderById(24);
System.out.println(order);

}

}

截图:





                                       
WSDL文档结构图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: