Rafael Borja
Rafael Borja

Reputation: 4907

Mule ESB - How to process a JSON request from a Webpage in a Java Component

I'm trying really hard to build the following scenario in Mule ESB 3: 1 - Send a Ajax request sending a JSON object to an AJAX endpoint in Mule ESB 2 - Map this JSON in a Java POJO using Json "Object to Json" component 3 - Invoke a Java method passing this converted object

I searched a lot of pages, but none has such a scenario. All the references doesn't have a complete example.

I know it's a very simple scenario, but its really, really hard to make such a dumb thing work in Mule ESB.

Upvotes: 1

Views: 396

Answers (2)

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

Mule has JSON-to-Object transformer which can be used to get JSON elements. So, when you receive a JSON input in your flow, you extract it using JSON-to-Object transformer and using return class as java.lang.Object or java.util.List or java.util.HashMap depending on your JSON.

For example if your JSON is following :-

{
  "token" : 123,
  "id" : 456,
  "email" : "[email protected]",
  "status" : "Success"
}

Now, to extract the elements, you need to use :-

<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />

And then can extract like :- #[message.payload.email] or #[message.payload.status]

Upvotes: 2

David Dossot
David Dossot

Reputation: 33413

Create a flow with an inbound HTTP endpoint, then a JSON to object transformer (not the other way around as you said) then either a component or invoke message processor, depending on how your Java object is designed.

Upvotes: 1

Related Questions