Apache CXF中如何传输Java对象
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,这篇文章将为大家详细讲解有关Apache CXF中如何传输Java对象,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。怎样通过CXF来构建最基本的Web
千家信息网最后更新 2025年02月24日Apache CXF中如何传输Java对象
这篇文章将为大家详细讲解有关Apache CXF中如何传输Java对象,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
怎样通过CXF来构建最基本的Web Service,并且其中暴露的接口参数和返回值都是字符串,下面来看看一个稍微复杂一点的例子。
1. 首先是一个普通的pojo对象,用来表示一个实体类
package com.googlecode.garbagecan.cxfstudy.jaxws; import java.util.Date; public class Customer { private String id; private String name; private Date birthday; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this); } }
2. 创建Web Service接口类
package com.googlecode.garbagecan.cxfstudy.jaxws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface CustomerService { @WebMethod @WebResult Customer findCustomer(@WebParam String id); }
3. 创建Web Service接口的实现类
package com.googlecode.garbagecan.cxfstudy.jaxws; import java.util.Calendar; public class CustomerServiceImpl implements CustomerService { public Customer findCustomer(String id) { Customer customer = new Customer(); customer.setId("customer_" + id); customer.setName("customer_name"); customer.setBirthday(Calendar.getInstance().getTime()); return customer; } }
4. 下面是Server端的代码
package com.googlecode.garbagecan.cxfstudy.jaxws; import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class MyServer { private static final String address = "http://localhost:9000/ws/jaxws/customerService"; public static void main(String[] args) throws Exception { // http://localhost:9000/ws/jaxws/customerService?wsdl JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); factoryBean.setServiceClass(CustomerServiceImpl.class); factoryBean.setAddress(address); factoryBean.create(); } }
5. 下面是Client端的代码
package com.googlecode.garbagecan.cxfstudy.jaxws; import java.net.SocketTimeoutException; import javax.xml.ws.WebServiceException; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class MyClient { public static void main(String[] args) throws Exception { JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.setAddress("http://localhost:9000/ws/jaxws/customerService"); factoryBean.setServiceClass(CustomerService.class); Object obj = factoryBean.create(); CustomerService customerService = (CustomerService) obj; try { Customer customer = customerService.findCustomer("123"); System.out.println("Customer: " + customer); } catch(Exception e) { if (e instanceof WebServiceException && e.getCause() instanceof SocketTimeoutException) { System.err.println("This is timeout exception."); } else { e.printStackTrace(); } } } }
6.测试
首先运行MyServer类,然后运行MyClient类来验证Web Service。
关于Apache CXF中如何传输Java对象就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
对象
接口
传输
代码
内容
文章
更多
知识
端的
篇文章
运行
不错
复杂
普通
例子
参数
字符
字符串
实体
质量
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
电信行业数据库产品第一期测评
php连数据库
苹果6s刷机无法接通激活服务器
南宫软件开发价格实惠
我国网络安全态势的状态
供电所网络安全拓展图
深圳市寻城问计网络技术
如何保护数据库服务器
注意网络安全英文
二道区智能网络技术有哪些
网络安全是重中之重包括什么领域
所有数据库主页
qt数据库模型
远程服务器未找到怎么办
网络安全攻防竞赛中心
北京举行的网络安全大会
软件开发html5培训
东莞聊天软件开发编程
服务器为什么抽风
转回原服务器能带多少多少
网络安全手抄报图画大全
特岗教师的服务器为
数据库表中添加主键id
2003服务器架设
数据库拷贝表字段
软件开发个人简历封面下载
livac数据库
网络安全知识竞赛有一等奖吗
c 的网络安全系统设计
汽车网络技术题库