Question

I'm having some trouble finding out how to create a custom transformer that can input and output a file in Mule 3.2. I have prototyped the code for the transformation and that works fine but I can't find any documentation on how to take in a file in a transformer.

Here is what I have so far as I'm but even this throws an error:

@ContainsTransformerMethods
  public class xmlToJson {

  @Transformer
  public File xmlIn(File file) {
    // logic to go here
    return file;
  }
}

Here is the exception that is thrown:

ERROR 2012-06-27 14:08:37,664 [main] org.mule.tooling.server.application.
ApplicationDeployer: null
java.lang.IllegalStateException: Cannot convert value of type [convert.xmlToJson]
to required type [org.mule.api.processor.MessageProcessor] for property 'messageProcessors[0]': no matching editors or conversion strategy found

I can't seem to find any documentation or tutorials that show how to structure a custom transformer to take in a file.

Was it helpful?

Solution

The annoteted transformer are usually intended for automatic transformation as explained here:

http://www.mulesoft.org/documentation/display/MULE3USER/Creating+Custom+Transformers

What would probably fit better you use case is creating a custom transforme by extending the AbstractTransformer as explained here:

http://www.mulesoft.org/documentation/display/MULE3USER/Creating+Custom+Transformer+Class

You can find a good tutorial about how to use either of this approaches at the following link

http://www.mulesoft.org/documentation/display/MULE3EXAMPLES/Invoking+Component+Methods

OTHER TIPS

In order to create a custom transformer with a custom logic you need to create your custom class which extends AbstractMessageTransformer and then you should override the transformMessage() from this Abstract class. After that only you can provide your custom class in any of the transformer tags. The annotated transformers will be registered within Mule and they would get called automatically if Mule needs to transform from sourceType to returnType.

@ContainsTransformerMethods
public class MyCustomTransformers
{
 @Transformer
  public URL stringToURL(String string) throws MalformedURLException
   {
     return new java.net.URL(string);
   }
}

Here the sourceType is String and returnType is URL. So whenever Mule itself needs to convert from a String to a URL, this transformer will be used.

Here is a nice link for creating custom transformers in Mule

http://javacodinggeeks.blogspot.in/2015/05/writing-custom-transformers-in-mule.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top