3

How to convert ManagedCursorStreamProvider to Json object in mule. I have written a java method which takes the Json Object as input

Request Payload:
{ a: "one",
b : "two"}

Invoke static

arg0 : payload

Java Function called using invoke static

public static func(JsonObject json){
}

I am getting the following error:

Expected arguments are [com.google.gson.JsonObject jsonObject] and invocation was attempted with arguments [org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider arg0]. No suitable transformation was found to match the expected type for the parameter [jsonObject].

UPDATE: I have updated my java method to accept String as input.

"Cannot coerce Object { encoding: UTF-8, mediaType: application/json; charset=UTF-8, mimeType: application/json, raw: org.mule.weave.v2.el.SeekableCursorStream@868075a } (org.mule.weave.v2.el.MuleTypedValue@7c0c5e89) to String

1| arg0 : vars.req as String
          ^^^^^^^^^^^^^^^^^^
Trace:
  at main (line: 1, column: 8)" evaluating expression: "arg0 : vars.req as String".
HMT
  • 2,093
  • 1
  • 19
  • 51
  • Why are you using GSON and Java instead of handling that payload with DataWeave? – afelisatti Apr 08 '20 at 12:30
  • @afelisatti Thanks for the response. I want to encrypt the request payload I have written the encryption logic in Java. – HMT Apr 08 '20 at 15:06
  • I wouldn't use a String, InputStream is more like it. Particularly if the data is coming from an HTTP operation. One thing you could also try if you have a static method in Java, is calling that through DataWeave directly. – afelisatti Apr 09 '20 at 03:50

1 Answers1

2

Mule doesn't know how to convert to a GSON JsonObject. You can use DataWeave to transform it into a Java map. Alternatively, you can change the argument of the Java method to String and Mule will transparently convert the stream to a String. Be sure to use the latest version of the Java module.

If you want to convert to a custom type of object you will need to implement it yourself in Java.

Yuto
  • 148
  • 1
  • 2
  • 11
aled
  • 21,330
  • 3
  • 27
  • 34
  • Thanks for the response. I want to encrypt the request payload I have written the encryption logic in Java, but let me try passing it as String. – HMT Apr 08 '20 at 15:05
  • Please review the Updated part in my question – HMT Apr 08 '20 at 15:27
  • Is the error happening using the last version of the Java Module? Earlier versions had a bug that caused incorrect serialization to Strings. – aled Apr 08 '20 at 19:13
  • Yes , this is the latest version of java – HMT Apr 08 '20 at 19:50
  • Sorry, I just want to confirm the version of the Java Module in the application is the latest one. Currently the version is 1.2.5 (https://docs.mulesoft.com/release-notes/connector/java-module). Just to be sure we are on the same page, I'm not talking about the version of the Java JDK used to execute the Mule Runtime. – aled Apr 08 '20 at 20:38
  • The version I am using is 1.2.6 – HMT Apr 09 '20 at 03:52