SOAP Web-Services - eSpace

Document Sample
SOAP Web-Services - eSpace Powered By Docstoc
					    SOAP Web-Serv ices
Using Spring Framework, CXF and JAXB
Agenda


 • Review; Web-Services, SOAP, WSDL, and UDDI
 • What is CXF? Why?
 • CXF Transports, Bindings, and Formats
 • CXF Data Binding
 • The Java API for XML Web Services (JAX-WS)
 • The Java API for XML Binding (JAXB)
 • Mapping POJOs using JAXB Annotations
 • Developing services approaches
 • Java First Development steps
 • Required libraries
 • References
 • Let’s go to eclipse and see some code
Review; Web-Services, SOAP, WSDL and UDDI


Web-Services
• Based on SOA (Service Oriented Architecture)
• The term Web Services refers to the technologies that allow for
  making connections between services over the web connection
  (SOAP/REST)
SOAP (Simple Object Access Protocol)
•   SOAP is   a communication protocol
•   SOAP is   a format for sending messages
•   SOAP is   designed to communicate via Internet (HTTP/HTTPS)
•   SOAP is   platform independent
•   SOAP is   language independent
•   SOAP is   based on XML
•   SOAP is   simple and extensible
Review; Web-Services, SOAP, WSDL and UDDI



WSDL (Web Service Description Language)
• Well Defined service; Functional and Non-Functional Description
• WSDL describes service interface and implementation

UDDI (Universal Description, Discovery, and Integration)
• Need for a way to publish and find web services
• Provides organization of online services
• Listing of worldwide internet businesses
What is CXF?



• It is the combination of the two projects: Celtix (ESB)
  developed by IONA and XFire developed by codehaus
  working together at Apache Software Foundation.
• Apache CXF is an open source services framework.
• CXF helps you build and develop services using frontend
  programming APIs, like JAX-WS.
• These services can speak a variety of protocols such as
  SOAP, XML/HTTP, RESTful HTTP, or CORBA and work
  over a variety of transports
Why CXF?



CXF includes a broad feature set, but it is primarily
  focused on the following areas:

• Web Services Standards Support (SOAP, WSDL, MTOM)
• Frontends (JAX-WS)
• Ease of use (APIs for code-first services, Maven plug-
  ins, Spring 2.0 XML support)
• Binary and Legacy Protocol Support (not only XML, but
  JSON and CORBA also)
CXF Transports, Binding and Formats


• Bindings
   – SOAP
   – REST/HTTP
• Formats: XML and JSON
• Transports
   – HTTP (Stand alone or Servlet for servlet-based
     applications)
   – Jabber transports
   – CXF includes a "local" transport.
• Extensibility API allows additional bindings for CXF, enabling
  additional message format support such as CSV.
CXF Data Binding



CXF includes two data-bindings:
• JAXB Data-Binding (CXF uses JAXB 2.x as its default data
  binding)
• Aegis data-binding which will turn nearly any java object
  into something that can be represented using schema,
  including Maps, Lists, and un-annotated java types.
 MTOM (Message Transmission Optimization Mechanism)
  Attachments
    Many frameworks have support for MTOM - Axis2, JBoss WS,
     XFire, Microsoft's WCF, and more.
    Enabling MTOM is a rather simple process:
      Annotate your schema type or POJO
      Tell CXF that you wish to enable MTOM
The Java API for XML Web Services (JAX-WS)



• JAX-WS is a Java programming language API for creating
  web services.
• JAX-WS uses annotations.
• Supported by CXF.
• The Reference Implementation of JAX-WS is developed
  as an open source project and is part of project
  GlassFish, an open source Java EE application server
The Java API for XML Binding (JAXB)



• The CXF 2.0.x branch supplies JAXB 2.0
• JAXB uses Java annotation combined with files found on
  the class-path to build the mapping between XML and
  Java.
• JAXB supports both code-first and schema-first
  programming.
Mapping POJOs using JAXB Annotations



• @XmlType
  maps a class to a XML Schema type.
• @XmlAttribute
   Marks a field or property as serialized to an XML
  attribute. By default, fields serialize to XML elements.
• @XmlTransient
  Marks a field or property as unserializable. JAXB will
  ignore the transient field.
• @XmlMimeType
  Marks a field as MTOM attachment
Mapping POJOs using JAXB Annotations


@XmlType
public class myObject {
  protected String field1;

    @XmlTransient
    protected String field2;

    @XmlMimeType("application/octet-stream")
    protected DataHandler data;

    @XmlAttribute(name="attribute")
    private String attr1;
}
Developing Services Approaches



• Start with a WSDL contract and generate Java objects
  to implement the service
   – CXF: wsdl2java takes a WSDL document and generates
     fully annotated Java code from which to implement a
     service.
   – JAXB XML Schema files
• Start with a Java object and service enables it using
  annotations
   – Using JAX-WS Annotations
   – Using JAXB Annotations
Java First Development Steps



To create a service starting from Java you need to do
  the following:
1. Create a Service Endpoint Interface (SEI) that defines
   the methods you wish to expose as a service.
2. Add the required annotations to your code.
3. Generate the WSDL contract for your service
   – Using Java2WSDL
   – Use CXF Servlet
4. Publish the service (UDDI).
Java First Development Steps


Creating the SEI (Service Endpoint Interface)
There are two basic patterns for creating an SEI:

• Green fields development
  you are developing a new service from the ground up.

• Service enablement
  you typically have an existing set of functionality that is
  implemented as a Java class and you want to service enable
  it.
Java First Development Steps


Writing the SEI
• The SEI is a standard Java interface. It defines a set of
  methods that a class will implement.
• In the case of an SEI the methods defined are intended to be
  mapped to operations exposed by a service.
   – The SEI corresponds to a wsdl:portType element
   – The methods defined by the SEI correspond to
      wsdl:operation
   public interface HelloWorld
   {
      Public String sayHi(String text);
   }
Java First Development Steps


Implementing the SEI

package eg.com.espace;

public class HellowWorldImpl implements HellowWorld{
  public String sayHi (String text) {
        return “Hellow ” + text;
  }
}
Java First Development Steps


Annotating the code
Required annotations
                                 wsdl:portType
import javax.jws.*;
                                        wsdl:service
@WebService(
   name="hellowWorld“,
   serviceName="hellowWorldService",
   wsdlLocation="http:\\eg.com.espace\hellowWorld.wsdl",
   portName="hellowWorldPort")
public interface HellowWorld
                                                  wsdl:port name
{ public String sayHi(String text); }
Java First Development Steps



                               Required when using SEI
import javax.jws.*;

@WebService(endpointInterface="eg.com.espace.HellowWorld”)
public class HellowWorldImpl implements HellowWorld{
  public String sayHi (String text) {
        return “Hellow ” + text;
  }
}
Java First Development Steps


Annotating the code
Optional annotations

@WebService(name="hellowWorld")
public interface HellowWorld{
  @WebMethod(operationName="sayHi2")
  @WebResult(name="hiMessage")
  public String sayHi(
        @WebParam(name=“msg")
        String text
  );
}
Java First Development Steps


Integration with Spring Framework
1. Add the CXF servlet transport to web.xml
<servlet>
   <servlet-name>CXFServlet</servlet-name>
   <servlet-class>
        org.apache.cxf.transport.servlet.CXFServlet
   </servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>CXFServlet</servlet-name>
   <url-pattern>/services/*</url-pattern>
</servlet-mapping>
Java First Development Steps


Integration with Spring Framework
2. Publishing and endpoint with XML, add the following in
   applicationContext.xml

<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=“hellowWorld”
    implementor="eg.com.espace.HellowWorldImpl“
    address="/hellowWorld"/>
Java First Development Steps


Integration with Spring Framework
JAXWS endpoint schema also supports many child elements
• jaxws:inInterceptors The incoming interceptors for this
  endpoint.
• jaxws:inFaultInterceptors The incoming fault interceptors for
  this endpoint.
• jaxws:outInterceptors The outgoing interceptors for this
  endpoint.
• jaxws:outFaultInterceptors The outgoing fault interceptors for
  this endpoint.
Java First Development Steps


Integration with Spring Framework
<jaxws:endpoint id="helloWorld"
   implementor="eg.com.espace.HelloWorldImpl"
   address="/HelloWorld">
   <jaxws:inInterceptors>
        <bean class="eg.com.espace.SomeInterceptor"/>
        <ref bean="anotherInterceptor"/>
   </jaxws:inInterceptor>
   <jaxws:properties>
        <entry key="mtom_enabled" value="true"/>
   </jaxws:properties>
</jaxws:endpoint>
Java First Development Steps


Generate the WSDL contract for your service




       http://localhost:8080/services/hellowWorld?wsdl
Required Libraries
References



• CXF User's Guide

• Web Services Tutorial with Apache CXF

• Web Services with Spring 2.5 and Apache CXF
  2.0
Thank You
        Wael M. Shaban
Senior Dev., eSpace Technologies

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:18
posted:11/14/2012
language:English
pages:28