I have built a Mule 4 module using the Mule SDK that internally makes use of com.googlecode.json-simple
. To create the project I used Maven archetype org.mule.extensions:mule-extensions-archetype-maven-plugin:1.2.0
as explained in https://docs.mulesoft.com/mule-sdk/1.1/getting-started
I'm able to build the project and include it in Anypoint Studio 7.8 and make use of the operations in my Mule application, I can configure them, update its properties in the UI, but whenever I run the application is is failing with java.lang.NoClassDefFoundError
because it cannot find the library.
What additional configuration should I do to ensure that the library is available at runtime?
Error:
ERROR 2021-04-09 06:33:48,410 [WrapperListener_start_runner] org.mule.runtime.module.deployment.internal.DefaultArchiveDeployer: Failed to deploy artifact [project1]
org.mule.runtime.deployment.model.api.DeploymentException: Failed to deploy artifact [project1]
Caused by: java.lang.NoClassDefFoundError: com/google/gson/JsonObject
at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_232]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[?:1.8.0_232]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[?:1.8.0_232]
at org.mule.runtime.module.extension.internal.util.IntrospectionUtils.lambda$getMethodsStream$14(IntrospectionUtils.java:923) ~[mule-module-extensions-support-4.3.0-20210119.jar:4.3.0-20210119]
at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:269) ~[?:1.8.0_232]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[?:1.8.0_232]
at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1556) ~[?:1.8.0_232]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[?:1.8.0_232]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[?:1.8.0_232]
pom.xml for module
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mule.extension</groupId>
<artifactId>mule-basic-extension</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>mule-extension</packaging>
<name>My Extension</name>
<parent>
<groupId>org.mule.extensions</groupId>
<artifactId>mule-modules-parent</artifactId>
<version>1.1.3</version>
</parent>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
Edit: Operations class
public class MyOperations {
Logger logger = LoggerFactory.getLogger(MyOperations.class);
/**
* Example of an operation that uses the configuration and a connection instance to perform some action.
*/
@MediaType(value = ANY, strict = false)
public String retrieveInfo(@Config MyConfiguration configuration, @Connection MyConnection connection){
return "Using Configuration [" + configuration.getConfigId() + "] with Connection id [" + connection.getId() + "]";
}
/**
* Example of a simple operation that receives a string parameter and returns a new string message that will be set on the payload.
*/
@MediaType(value = ANY, strict = false)
public String sayHi(String person) {
return buildHelloMessage(person);
}
/**
* Returns a JSON representation of the input
*/
@Throws(ExecuteErrorsProvider.class)
@MediaType(value = MediaType.APPLICATION_JSON, strict = false)
public String parseInput(String record) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("key", record);
return jsonObject.toString();
}
/**
* Private Methods are not exposed as operations
*/
private String buildHelloMessage(String person) {
return "Hello " + person + "!!!";
}
}
It turns out there was an issue with the JSON library that I was using in my project.
I replaced this dependency
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
with this one and I was able to create operations that return JSON
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>