Apache CXF实战之三 传输Java对象
2013-06-08 09:35
691 查看
本文链接:http://blog.csdn.net/kongxx/article/details/7527094
Apache CXF实战之一 Hello World Web Service
Apache CXF实战之二 集成Sping与Web容器
前面两篇文章介绍了怎样通过CXF来构建最基本的Web Service,并且其中暴露的接口参数和返回值都是字符串,下面来看看一个稍微复杂一点的例子。
1. 首先是一个普通的pojo对象,用来表示一个实体类
[java]
view plaincopyprint?
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);
}
}
[java]
view plaincopyprint?
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);
}
[java]
view plaincopyprint?
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;
}
}
[java]
view plaincopyprint?
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();
}
}
[java]
view plaincopyprint?
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();
}
}
}
}
首先运行MyServer类,然后运行MyClient类来验证Web Service。
Apache CXF实战之一 Hello World Web Service
Apache CXF实战之二 集成Sping与Web容器
前面两篇文章介绍了怎样通过CXF来构建最基本的Web Service,并且其中暴露的接口参数和返回值都是字符串,下面来看看一个稍微复杂一点的例子。
1. 首先是一个普通的pojo对象,用来表示一个实体类
[java]
view plaincopyprint?
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);
}
}
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接口类
[java]
view plaincopyprint?
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);
}
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接口的实现类
[java]
view plaincopyprint?
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;
}
}
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端的代码
[java]
view plaincopyprint?
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();
}
}
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端的代码
[java]
view plaincopyprint?
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();
}
}
}
}
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对象
- Apache CXF实战之三 传输Java对象
- Apache CXF实战之三 传输Java对象
- Apache CXF实战之三 传输Java对象
- Apache CXF实战之三 传输Java对象
- Apache CXF实战之二 传输pojo对象
- JAVA并发实战 第四章 对象的组合
- 使用Mina传输Java对象
- Java Socket实战之三 传输对象
- 第一天:Java源码级实战速成(通过动手实战类、对象等,通过Spark和Hadoop案例代码和源码解析具体指知识的应用、深度详解匿名接口在Spark开发中的运用)
- Java Socket实战之五 使用加密协议传输对象
- Java Socket实战之五 使用加密协议传输对象
- Java Socket实战-多线程通信 传输对象 传输压缩对象
- SOCKET实现对象传输【java实现】
- Apache CXF实战之七 使用Web Service传输文件
- javaWeb实战教程2-servlet内置对象和错误处理
- Java Socket实战之四 传输压缩对象
- webservice delphi客户端 java服务器端 传输简单和复杂对象 小demo
- 我的JAVA工具之网络传输对象
- Java Socket实战之三 传输对象