MuleSoft Consuming Soap Web Service - CXF jax-ws-client Module

  • View
    2.000

  • Download
    6

  • Category

    Software

Preview:

Citation preview

CONSUMING SOAP WEB SERVICE

- MULESOFT CXF JAX-WS-CLIENT MODULE

Vince Jason Soliza

Mulesoft CXF Jax-WS Client Module

When using CXF inside of Mule, there are several different ways to consume web service.

One is the WSDL First CXF JAX-WS Client: It builds a message processor which can use a JAX-WS client generated from WSDL.

JAX-WS-CLIENT WSDL / CONTRACT FIRST APPROACH

Create Stub Client with CXF: wsdl2javaFirst we need to setup our maven configuration to create a stub client. CXF includes the wsdl2java utility that can generate Java stub client code to call any method on the service, and marshal and un-marshal request parameters and responses as Java objects for further processing. This generated stub client is the core of your connector.

Create Stub Client with CXF: wsdl2java

The Java source files generated correspond to the service as described by the contents of the WSDL.

Create 2 Flows:Main Flow & CXF Client Flow

Main Flow Components• HTTP Listener

– Accepts the request

• Transform Message – Create request for web service consumer the output is XML

• XML-to-JAXB-Object – Transform XML to JAXB-Object to be consumed by the JAX-WS-Client

• Flow Reference– Reference to the Flow of EchoServiceFlow

• Transform Message– Parse the response of EchoServiceFlow to XML

CXF Client Components• CXF

– Jax-ws-client configuration

• HTTP request– http request hold the configuration for the target endpoint

Request ConfigurationWe used Transform Message to create a SOAP request, cxf:jax-ws-client requires jaxb-object as the acceptable request so we added xml-to-jaxb-object transformer after it.

Request ConfigurationCheck in debug mode, the payload is an instance of EchoRequest.

Request ConfigurationCode Snippet:

<dw:transform-message doc:name="Transform Message"><dw:set-payload><![CDATA[%dw 1.0

%output application/xml%namespace echo http://www.whiteskylabs.com/wsdl/echo/---echo#EchoRequest: {

EchoInfo: {Id: "1345",Name: "Mario Luigi",Description: "Mario Bros",OtherInfo: "Legendary"}

}]]></dw:set-payload></dw:transform-message><mulexml:jaxb-xml-to-object-transformer

returnClass="com.whiteskylabs.wsdl.echo.EchoRequest" jaxbContext-ref="JAXB_Context"doc:name="XML to JAXB Object" />

cxf:jax-ws-client ConfigurationConfigure the client with the following properties.

• clientClass: The client class generated by CXF, which extends javax.xml.ws.Service.

• port: The WSDL port to use for communicating with the service

• wsdlLocation: The location of the WSDL for the service. Since we are using WSDL first client, CXF uses this to configure the client.

• operation: The operation name to invoke on the web service.

cxf:jax-ws-client ConfigurationWe put cxf:jax-ws-client into a new private flow, to wrap it as a SOAP web service consumer.

code snippet:

<flow name="EchoServiceFlow"><cxf:jaxws-client operation="echo"

clientClass="com.whiteskylabs.wsdl.echo.EchoService_Service" port="EchoServicePort"

wsdlLocation="classpath:/EchoService.wsdl" doc:name="CXF"soapVersion="1.2" />

<http:request config-ref="HTTP_Request_Configuration"path="/echo-ws/ws/EchoService" method="POST" doc:name="HTTP" />

</flow>

Response ConfigurationThe cxf:jax-ws-client response is also an object, but unlike with request it’s not in the form of JAXB-Object. We can directly use the Transform Message to parse it and create an XML response.

Response ConfigurationCheck in debug mode, the payload is an instance of EchoResponse.

Response Configurationcode snippet:

<dw:transform-message doc:name="Transform Message"><dw:set-payload><![CDATA[%dw 1.0

%output application/xml%namespace echo http://com.whiteskylabs/wsdl/echo/---{

echo#EchoResponse: {echoResult: {echoInfo: {id: payload.echoResult.echoInfo.id,name: payload.echoResult.echoInfo.name,description: payload.echoResult.echoInfo.description,otherInfo: payload.echoResult.echoInfo.otherInfo}}}

}]]></dw:set-payload></dw:transform-message>

Test the application• Run the application in Anypoint Studio.• Send request through HTTP using Postman, browser or any client you

prefer.• We can see in the screenshot below, the response of the soap web service

we consumed using web service consumer.

Summary

This slide describes how to consume web services using the CXF jax-ws-client message processor using WSDL first approach.

It is still recommended to use the Web Service Consumer component instead of this whenever possible.

QUESTIONS?Please leave a comment