The previous two articles introduced how to build the most basic Web Service through CXF, And the exposed interface parameters and return values are strings. Let's take a look at a slightly more complex example.
1. A common pojo object is used to represent an object class.
- 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. Create a Web Service Interface Class
- 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. Create a Web Service Interface implementation class
- 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. The following is the Server code.
- 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. The following is the Client code.
- 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. Test
First, run the MyServer class, and then run the MyClient class to verify the Web Service.
Series of articles]