Creating Web Services in Mule ESB is really easy, you just have to sit down and let CXF do all the magic for you. There’s just one thing you have to do by yourself – code your service logic or, in other words, program what the service will do.
You have two options: program Java classes that encapsulate all the logic and generate a response for the service, or, route the message payload to Mule’s flow to keep doing awesome things with Mule’s components. In this example I will show you option number one.
Let’s begin with the fun part. The following example shows how to expose a SOAP Web Service using Apache CXF that is within Mule ESB.
mule-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd "> <flow name="SimpleCXFServiceFlow1" doc:name="SimpleCXFServiceFlow1"> <http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:9090/simpleCXFService" doc:name="HTTP" /> <cxf:jaxws-service serviceClass="com.marco.tello.service.ISimpleCXFService" doc:name="SOAP" /> <component class="com.marco.tello.service.SimpleCXFService" doc:name="Java" /> </flow> </mule> |
ISimpleCXFService.java
1 2 3 4 5 6 7 |
package com.marco.tello.service; public interface ISimpleCXFService { public String sayHello(String name); } |
SimpleCXFService.java
1 2 3 4 5 6 7 8 9 10 |
package com.marco.tello.service; public class SimpleCXFService implements ISimpleCXFService { @Override public String sayHello(String name) { return "Hello" + name + ", this a simple CXF service."; } } |
If you are using Mule Studio your flow should look like this:
The Mule Studio project source code is SimpleCXFService or you can find the project on Github
Hope you find this useful 😉
6 comments: On Simple Web Service In Mule ESB 3.3
Hi. Please, how about the performance of this solution? Tks!
Hi ,thanks for this excellent information.CAn you please send how to build a java class that will call the mule flow endpoint in it.And how to expose this java class as a webservice.Urgent help.
Thank you
Hi Marco,
Please tell me how this example could be expanded for reading data from a database.
My first thought is to add code in the ISimpleCXFService class to connect to the database and retrieve information but I am not sure this is the correct way.
On the other hand, it is not clear to me what objects I should add to the flow to have it go to the database and return the results of a select statement.
Thank you
INFO 2014-04-23 18:03:08,183 [main] org.mule.module.launcher.application.DefaultMuleApplication: App ‘simplecxfservice’ never started, nothing to dispose of
Exception in thread “main” org.mule.module.launcher.DeploymentInitException: NullPointerException:
at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:219)
at org.mule.module.launcher.application.ApplicationWrapper.init(ApplicationWrapper.java:64)
at org.mule.module.launcher.DefaultMuleDeployer.deploy(DefaultMuleDeployer.java:47)
at org.mule.tooling.server.application.ApplicationDeployer.main(ApplicationDeployer.java:127)
Caused by: org.mule.api.config.ConfigurationException: Error creating bean with name ‘SimpleCXFServiceFlow1’: Invocation of init method failed; nested exception is org.mule.api.lifecycle.InitialisationException: null (org.mule.api.lifecycle.InitialisationException) (org.mule.api.config.ConfigurationException)
at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:52)
at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
at org.mule.context.DefaultMuleContextFactory.createMuleContext(DefaultMuleContextFactory.java:84)
at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:207)
… 3 more
Caused by: org.mule.api.config.ConfigurationException: Error creating bean with name ‘SimpleCXFServiceFlow1’: Invocation of init method failed; nested exception is org.mule.api.lifecycle.InitialisationException: null (org.mule.api.lifecycle.InitialisationException)
at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:52)
at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
at org.mule.config.builders.AutoConfigurationBuilder.autoConfigure(AutoConfigurationBuilder.java:101)
at org.mule.config.builders.AutoConfigurationBuilder.doConfigure(AutoConfigurationBuilder.java:57)
at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
… 6 more
Caused by: org.mule.api.lifecycle.InitialisationException: Error creating bean with name ‘SimpleCXFServiceFlow1’: Invocation of init method failed; nested exception is org.mule.api.lifecycle.InitialisationException: null
at org.mule.registry.AbstractRegistry.initialise(AbstractRegistry.java:117)
at org.mule.config.spring.SpringXmlConfigurationBuilder.createSpringRegistry(SpringXmlConfigurationBuilder.java:119)
at org.mule.config.spring.SpringXmlConfigurationBuilder.doConfigure(SpringXmlConfigurationBuilder.java:73)
at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
… 10 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘SimpleCXFServiceFlow1’: Invocation of init method failed; nested exception is org.mule.api.lifecycle.InitialisationException: null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.mule.config.spring.SpringRegistry.doInitialise(SpringRegistry.java:89)
at org.mule.registry.AbstractRegistry.initialise(AbstractRegistry.java:109)
… 13 more
Caused by: org.mule.api.lifecycle.InitialisationException: null
at org.mule.module.cxf.config.FlowConfiguringMessageProcessor.initialise(FlowConfiguringMessageProcessor.java:100)
at org.mule.processor.chain.AbstractMessageProcessorChain.initialise(AbstractMessageProcessorChain.java:83)
at org.mule.construct.AbstractFlowConstruct.initialiseIfInitialisable(AbstractFlowConstruct.java:318)
at org.mule.construct.AbstractPipeline.doInitialise(AbstractPipeline.java:212)
at org.mule.construct.AbstractFlowConstruct$1.onTransition(AbstractFlowConstruct.java:113)
at org.mule.construct.AbstractFlowConstruct$1.onTransition(AbstractFlowConstruct.java:107)
at org.mule.lifecycle.AbstractLifecycleManager.invokePhase(AbstractLifecycleManager.java:141)
at org.mule.construct.FlowConstructLifecycleManager.fireInitialisePhase(FlowConstructLifecycleManager.java:81)
at org.mule.construct.AbstractFlowConstruct.initialise(AbstractFlowConstruct.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
… 24 more
Caused by: java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at org.apache.cxf.jaxws.support.JaxWsServiceConfiguration.isWebMethod(JaxWsServiceConfiguration.java:174)
at org.apache.cxf.jaxws.support.JaxWsServiceConfiguration.isOperation(JaxWsServiceConfiguration.java:213)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.isValidMethod(ReflectionServiceFactoryBean.java:1896)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.createInterface(ReflectionServiceFactoryBean.java:940)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:439)
at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:690)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:511)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:245)
at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:205)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)
at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:159)
at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:211)
at org.mule.module.cxf.builder.AbstractInboundMessageProcessorBuilder.build(AbstractInboundMessageProcessorBuilder.java:210)
at org.mule.module.cxf.builder.AbstractInboundMessageProcessorBuilder.build(AbstractInboundMessageProcessorBuilder.java:66)
at org.mule.module.cxf.config.FlowConfiguringMessageProcessor.initialise(FlowConfiguringMessageProcessor.java:96)
… 39 more
Pingback: review buy movie ()
Pingback: Winthruster Crack ()