LDSTechForumProjects

Web Services with Apache CXF - Part 1

Training Date: 17 Aug 2011

Objectives

At the end of this presentation, the participant will be able to:

  • Describe the role of Apache CXF as a framework for building and consuming web services.
  • Identify the basic characteristics of a SOAP web service.
  • Identify the terms used to describe the various components of a SOAP web service.
  • Describe the advantages and disadvantages of SOAP over other web service protocols, namely REST.
  • Be able to produce a SOAP web service with Apache CXF in a Java Stack web application.
  • Be able to consume a SOAP web service with Apache CXF from a Java Stack QA module.
  • Be able to identify the purpose and components of a SOAP web service's WSDL document.

Slide Deck

Media:CXF-Training-Part-1.pptx‎

Introduction

Video Presentation

Get Adobe Flash player

SOAP

Video Presentation

Get Adobe Flash player

Lab 1: An Example JAX-WS SOAP Web Service

  1. Launch the Java Stack Starter
  2. Choose the Web Service Provider project type.
  3. Enter the following settings:
    • Project Name:
       training-cxf 
    • Package Name:
       org.lds.training.cxf 
    • Project Location:
       C:\Dev\Workspace\Training  (or anywhere you want)
  4. Click Next
  5. Select the Security property group and uncheck Use LDS Account Authentication.
  6. Select the Service Layer property group and choose Embedded as the database platform.
  7. Select the Web Services property group and choose SOAP as the CXF web service type.
  8. Click Next and Build
  9. Import the training-cxf project into your IDE as a Maven project. (Also ensure that you import all child projects: training-cxf-api, training-cxf-deploy, training-cxf-qa, and training-cxf-web)
    1. To do this in the LDSTech IDE Go to File->Import->Maven->Existing Maven Project
    2. Choose the folder that stack starter just created for you
  10. Add the training-cxf-web project to an LDSTech (Tomcat 6) web server and run it.
  11. Browse to the web service WSDL: http://localhost/training-cxf/Services/example?wsdl
  12. Note that the web service endpoint address is: http://localhost/training-cxf/Services/example without the ?wsdl query string.
  13. Locate all 3 web service operations in the WSDL
    • Observe how parameter types are defined in the XML.
    • Observe how the result is defined in the XML.
  14. Compare the WSDL to the SEI in the training-cxf-api module: org.lds.training.cxf.ws.ExampleWebService

Video Presentation

Get Adobe Flash player

Namespace Handlers

Video Presentation

Get Adobe Flash player

Lab 2: Producing a JAX-WS SOAP Web Service

  1. Define a new service endpoint interface (SEI) in the training-cxf-api module: org.lds.training.cxf.ws.HelloWebService
    • Paste the following:
package org.lds.training.cxf.ws;

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

@WebService(targetNamespace="http://schema.lds.org/training-cxf/hello/v1.0")
public interface HelloWebService {

	@WebMethod(action="Hello")
	public String hello(@WebParam(name="name") String name);
}
  1. Create a web service implementation class in training-cxf-web: org.lds.training.cxf.ws.HelloWebServiceSoapImpl
    • Paste the following:
package org.lds.training.cxf.ws;

import javax.jws.WebService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller("helloWebServiceSoap")
@Scope("singleton")
@WebService(endpointInterface="org.lds.training.cxf.ws.HelloWebService",
		targetNamespace="http://service.lds.org/training-cxf/hello/v1.0",
		serviceName="Hello",
		name="Hello")
public class HelloWebServiceSoapImpl implements HelloWebService {

	public String hello(String name) {
		return "Oh, I wish I were a little bar of " + name + "!";
	}
}
  1. Paste the following into your WEB-INF/training-cxf-servlet.xml Spring configuration:
<stack-ws:produce implementor="helloWebServiceSoap" secured="false" address="/hello"/>
  1. Rebuild and restart the web application.
  2. Browse to the web service WSDL: http://localhost/training-cxf/Services/hello?wsdl


Lab 3: Consuming a JAX-WS SOAP Web Service

  • Create an integration test in the QA module: org.lds.training.cxf.HelloServiceSoapIT
  • Paste the following:
package org.lds.training.cxf;

import javax.inject.Inject;
import javax.inject.Named;
import org.lds.training.cxf.ws.HelloWebService;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

@ContextConfiguration(locations={"/META-INF/spring/applicationContext-test.xml"})
public class HelloServiceSoapIT extends AbstractTestNGSpringContextTests  {

	@Inject
	@Named("helloWebServiceSoap")
	private HelloWebService helloWebService;

	@Test(groups="smoke")
	public void testCreateExample() {
		System.out.println("--------------------------------------------------");
		System.out.println(helloWebService.hello("SOAP"));
		System.out.println("--------------------------------------------------");
	}
}
  • Paste the following into the QA module's META-INF/spring/applicationContext-test.properties
    <stack-ws:consume id="helloWebServiceSoap"
        service-class="org.lds.training.cxf.ws.HelloWebService"
    	endpoint="http://localhost/training-cxf/Services/hello" />
  1. Paste the following into the QA module's testenv/default.properties resource file.
    • helloSoapEndpointUrl = ${appUrl}/Services/hello


Want to learn more?

See Java Stack Training | LDS Java Stack

This page was last modified on 12 July 2013, at 19:30.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.