新建Web工程,目录结构如图
Order.java实体类
public class Order {
private int id;
private String name;
private double price;
///...省略getter setter toString方法
public Order() {//无参构造函数
super();
}
}
OrderWS.java接口
package com.umgsai.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.umgsai.beans.Order;
@WebService
public interface OrderWS {
@WebMethod
public Order getOrderById(int id);
}
OrderWSImpl.java实现接口
package com.umgsai.ws.impl;
import com.umgsai.beans.Order;
import com.umgsai.ws.OrderWS;
public class OrderWSImpl implements OrderWS{
@Override
public Order getOrderById(int id) {
System.out.println("Server getOrderById:" + id);
return new Order(id, "Car", 200000);
}
}
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.umgsai.ws.impl.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://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WS_spring</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置beans --> <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> <!-- 所有的请求都会先经过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>
启动项目
http://localhost:8080/WS_spring/orderWS?wsdl 可以查看发布的服务
源码 http://yunpan.cn/cg4kLXaK8k5HS (提取码:7c34)
客户端调用上面发布的服务。
使用CXF自带的工具生成客户端代码
apache-cxf-3.0.1\bin>wsdl2java http://localhost:8080/WS_spring/orderWS?wsdl
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.umgsai.ws.OrderWS" address="http://localhost:8080/WS_spring/orderWS" /> </beans>
client.java 客户端代码
package com.umgsai.ws.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.umgsai.ws.Order;
import com.umgsai.ws.OrderWS;
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("client-beans.xml");
OrderWS orderWS = (OrderWS) applicationContext.getBean("orderClient");
Order order = orderWS.getOrderById(23);
System.out.println(order);
}
}
客户端打印结果
Order [id=23, name=Car, price=200000.0]
服务器端打印结果
Server getOrderById:23
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1563774