使用CXF框架开发一个简单的WebService
程序员文章站
2024-03-25 22:51:04
...
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文档结构图
上一篇: 使用MVC框架开发了一个实现用户登录注册注销的小项目
下一篇: 基于角色的访问控制(rbac)
推荐阅读
-
使用CXF框架开发一个简单的WebService
-
使用MVC框架开发了一个实现用户登录注册注销的小项目
-
使用CXF和Jersey框架来进行Java的WebService编程
-
使用CXF和Jersey框架来进行Java的WebService编程
-
C#使用TcpListener及TcpClient开发一个简单的Chat工具实例
-
C#使用TcpListener及TcpClient开发一个简单的Chat工具实例
-
使用Python的Bottle框架写一个简单的服务接口的示例
-
使用Python的Bottle框架写一个简单的服务接口的示例
-
使用Python的Tornado框架实现一个简单的WebQQ机器人
-
使用Python的Twisted框架实现一个简单的服务器