Mule parsing Incoming REST request - mule

I have a REST client that is preparing payload in JSON format and invoking a REST service. My job is to create the REST service in Mule. I need some information on how I can map the incoming Payload to a java object so that I can invoke the REST service component class and get the values passed in the JSON object. Does the payload after HTTP inbound endpoint already contain the JSON values sent by the client? In which case a simple JSON to Object mapper would map it as Hashmap?

You will most likely need to create a custom transformer for this use case if you have a special use case.
See: http://www.mulesoft.org/documentation/display/current/Creating+Custom+Transformer+Class
If you get sent JSON you can convert it into a custom class like this:
<json:json-to-object-transformer name="jsonToFruitCollection" returnClass="
org.mule.module.json.transformers.FruitCollection"/>

Alternatively You can also use ObjectMapper and can probably use a bean to map your JSON directly to your Java object in your Java class.
You can also use <json:json-to-object-transformer/> directly after your Http inbound endpoint, parse and get each element value in your Mule flow and store in variables. Then these variables can be passed into your Java class where you can map these to your Java object easily.
Both the approach will work fine

Related

Service Data Object(SDO) and Service Message Object(SMO)

can anyone explain what is Service Data Object(SDO) and Service Message Object(SMO)?
Questions:
1. what is the purpose of SDO and SMO?
2.how it works?
These concepts aren’t used with Mule, they seem to come from IBM. https://www.ibm.com/support/knowledgecenter/SSFTN5_8.5.7/com.ibm.wbpm.main.doc/topics/cwesb_sca_smo2.html
The equivalent of the SMO in Mule is the Mule Event which you can read about here: https://docs.mulesoft.com/mule-runtime/4.1/about-mule-event
A Mule event contains the core information processed by the runtime. It travels through components inside your Mule app following the configured application logic.
It’s basically an abstraction layer so you don’t have to deal with different protocols and transports.
A Mule Event is composed of these objects:
A Mule Message contains a message payload and its associated attributes.
Variables are Mule event metadata that you use in your flow.
A Http POST for example would be represented as an event.
The event payload would be the body data of the http request
Where as the http headers such as content-type would be attributes on the event.
Same for JMS. The message body would be the payload and the jms header would be attributes.
As for SDO, each SMO has an SDO. This is very specific to that IBM article and not relevant in Mule. But from what I understand it basically allows you to access your heterogenous business data in a common way. I guess Dataweave in Mule accomplishes this as Dataweave is the transformation and expression language in Mule, it allows you to query and transform data in the same way regardless of the data type, xml, Json, CSV and so on.

Mule ESB Flow to pass parameters in Calling SOAP Webservice

I have created on flow in MuleESB which is calling a web-service without any parameter just sending it username, password and token in a property and it is working fine.
But the second API I want to post some parameters while calling soap request but I don't know how to use it I tried to pass through set payload but no response.
use Webservice consumer and add a transform message component beofre it. by doing so you can automatically map all the parameters which are required by the SOAP webservice, as datasence will automaticall download the meta deta of the service using the WSDL file.
Make sure you select application/xml as content type in Postman or SOAP UI and select POST.
Use CXF and select Operation as Proxy Service ,Provide details. Selct and provode (WSDL,MTOM enabled,SOAP Headers ,SOAP 1.2)
Make sure you posting XML request "POST" method in allowed methods.
Use 2 transformers. XML to DOM and DOM to XML.
Log the request using
#[message.payloadAs(java.lang.String)]
Use a groovy script transformer to retreive the entire payload.
def userSoapRequest = new XmlSlurper().parseText(payload);
def userId = userSoapRequest.userId.text();
message.setInvocationProperty('userId', userId);
6.Retrieve userId like above and similarly for all the elements.
7.Process them as you want.
Hope this helps

Mule Cache Scope Error com.hazelcast.nio.serialization.HazelcastSerializationException

I am using Mule Cache scope to save JSON data (converted to string) retrieved from a REST call. All works fine locally but when deployed to the server which is clustered I get this error.
com.hazelcast.nio.serialization.HazelcastSerializationException: java.io.NotSerializableException: com.rabbitmq.client.impl.ChannelN
Not sure why it is trying to serialize rabbitmq channelN.
When cache scope includes any Rest calls (i.e HTTP Request configurations) , make sure you have Dataweave or Byte array to String or Object to String to make it as consumable payload. Because HTTP request configuration output will be "grizzly Bufferinputstream" which is not consumable payload and its not serializable

Getting SOAP request as POJO in Mule

In our project we expose a number of web-services that were generated from a wsdl. After generating them, I can see that the requests and responses are mapped to POJOs and when I am making the response, I just set a new POJO. This works really nice. However, I have a problem with the request. When we receive the request I expected that the payload will be a POJO mapping the parameters from the request. The payload becomes actually an array of objects. I can access the values but this is not very comfortable. You can take a look at the picture.
I can see that the under "Variables" in the method it is correctly matched to the POJO we would like to have. Is there some setting that I am missing somewhere so that we can get the payload to be mapped to correct POJO type?
Re-run the WSDL to Java codegen but this time with wrapper style disabled, see: https://cxf.apache.org/docs/wsdl-to-java.html#WSDLtoJava-wrapperstyle

Retrieve generic list in JSON format on client side

I send generic list of data by wcf service using REST. I understand that on server side it was serializing, and on client side deserializing. How can I get data before deserialization in JSON format to show it to user ?
Take a look at this page, and drop the automatic (de)serialization.
I use DownloadData method from WebClient class to invoke RESTful service and returned data was converted to string using Encoding class :)