Sunday, August 4, 2013

Camel-CXF: Java-First Service Implementation

Summarized from the documentation.

Web service interface is defined using a Service Endpoint Interface (SEI) and the WSDL is generated from it.

Steps
1. Implement & annotate the SEI.
2. Implement other requisite Java classes.
        - Any data types referenced by the SEI.
        - The implementation of the SEI. 3. Instantiate the Web service endpoint.
4. Generate the WSDL

1. Implement & annotate the SEI.

SEI is used as the;
• Base type of the Web service implementation (server side)—you define the Web service by implementing the SEI.
• Proxy type (client side)—on the client side, you use the SEI to invoke operations on the client proxy object.
• Basis for generating the WSDL contract—in the Java-first approach, you generate the WSDL contract by converting the SEI to WSDL.

In order to use the standard JAX-WS frontend, the SEI must be annotated with the @WebService annotation. Annotations are also used to customize the mapping from Java to WSDL.

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "http://demo.fusesource.com/wsdl/CustomerService/", name = "CustomerService", serviceName = "CustomerService", portName = "SOAPOverHTTP")
public interface CustomerService {
 public com.fusesource.demo.customer.Customer lookupCustomer(
   @WebParam(name = "customerId", targetNamespace = "") java.lang.String customerId);

 public void updateCustomer(
   @WebParam(name = "cust", targetNamespace = "") com.fusesource.demo.customer.Customer cust);

 public void getCustomerStatus(
   @WebParam(name = "customerId", targetNamespace = "") java.lang.String customerId,
   @WebParam(mode = WebParam.Mode.OUT, name = "status", targetNamespace = "") javax.xml.ws.Holder status,
   @WebParam(mode = WebParam.Mode.OUT, name = "statusMessage", targetNamespace = "") javax.xml.ws.Holder statusMessage);
}

2. Implement other requisite Java classes.

When you run the Java-to-WSDL compiler on the SEI, it converts not only the SEI, but also the classes referenced as parameters or return values. The parameter types must be convertible to XML, otherwise it would not be possible for WSDL operations to send or to receive those data types. In fact, when you run the Java-to-WSDL compiler, it is typically necessary to convert an entire tree of related classes to XML using the standard JAX-B encoding.

Each related class must have a default constructor .

public class Customer {
 protected String firstName;
 protected String lastName;
 protected String phoneNumber;
 protected String id;

 // Default constructor, required by JAX-WS
 public Customer() {
 }

 public Customer(String firstName, String lastName, String phoneNumber,
   String id) {
  super();
  this.firstName = firstName;
  this.lastName = lastName;
  this.phoneNumber = phoneNumber;
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String value) {
  this.firstName = value;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String value) {
  this.lastName = value;
 }

 public String getPhoneNumber() {
  return phoneNumber;
 }

 public void setPhoneNumber(String value) {
  this.phoneNumber = value;
 }

 public String getId() {
  return id;
 }

 public void setId(String value) {
  this.id = value;
 }
}

3. Instantiate the Web service endpoint.

In Apache CXF, you create a WS endpoint by defining a jaxws:endpoint element in XML. The WS endpoint is effectively the runtime representation of the Web service: it opens an IP port to listen for SOAP/HTTP requests, is responsible for marshalling and unmarshalling messages (making use of the
generated Java stub code), and routes incoming requests to the relevant methods on the implementor class.

jaxws:endpoint => To integrate a WS endpoint with a Java implementation class.
cxf:cxfEndpoint => To integrate a WS endpoint with a Camel route.

Creating a Web service in Spring XML consists of the following two steps:
1. Create an instance of the implementor class, using the Spring bean element.
2. Create a WS endpoint, using the jaxws:endpoint element.


 
 
 
 
 
 
 

4. Generate the WSDL

To generate a WSDL contract from your SEI, you can use either the java2ws command-line utility or the cxf-java2ws-plugin Maven plug-in. Plug-in approached is given below.

Add plugin to the POM .


 ...
 
  2.4.2-fuse-00-05
 
 
  install
  
   ...
   
    org.apache.cxf
    cxf-java2ws-plugin
    ${cxf.version}
    
     
      org.apache.cxf
      cxf-rt-frontend-jaxws
      ${cxf.version}
     
     
      org.apache.cxf
      cxf-rt-frontend-simple
      ${cxf.version}
     
    
    
     
      process-classes
      process-classes
      
       org.fusesource.demo.camelcxf.ws.server.CustomerService
       
       
        ${basedir}/../src/main/resources/wsdl/CustomerService.wsdl
       
       true
       true
      
      
       java2ws
      
     
    
   
  
 
 

No comments:

Post a Comment