Hey guys,
Sorry for this question, but I’m very new to sponge. I wanted to develop a plugin, sending requests to a RESTFUL Webservice (API).
After I read the docs, I was able to set up a working (very basic hello world) plugin. After that, I wanted to implement an API caller framework (because doing it in “plain” java isn’t what I want, haha)
Unfortunately, the current state of Sponge doesn’t provide a built-in solution. After some research, I decided to give Retrofit a try (should I use another tool, or is retrofit a good choice?).
When I start my sponge (vanilla) server, I get following error:
[17:22:24 ERROR] [Sponge]: Failed to load plugin: Plugin 1.0 (from mods\Plugin-1.0.jar)
java.lang.NoClassDefFoundError: retrofit2/Converter$Factory
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_192]
at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_192]
at org.spongepowered.server.plugin.VanillaPluginManager.loadPlugin(VanillaPluginManager.java:161) ~[VanillaPluginManager.class:1.12.2-7.1.2]
at java.util.ArrayList.forEach(Unknown Source) ~[?:1.8.0_192]
at org.spongepowered.server.plugin.VanillaPluginManager.loadPlugins(VanillaPluginManager.java:89) ~[VanillaPluginManager.class:1.12.2-7.1.2]
at org.spongepowered.server.SpongeVanilla.preInitialize(SpongeVanilla.java:120) ~[SpongeVanilla.class:1.12.2-7.1.2]
at net.minecraft.server.dedicated.DedicatedServer.handler$onServerLoad$bba000(SourceFile:1234) ~[nz.class:?]
at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(SourceFile:117) ~[nz.class:?]
at net.minecraft.server.MinecraftServer.run(SourceFile:434) ~[MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_192]
Caused by: java.lang.ClassNotFoundException: retrofit2.Converter$Factory
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:191) ~[launchwrapper-1.12.jar:?]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_192]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_192]
... 10 more
Caused by: java.lang.NullPointerException
It’s funny that I get an error, because I really tried to stick to Retrofit’s doc’s example (this lets me believe, that there has to be something different causing problems).
Maybe I should also show you my gradle.build file:
plugins {
id 'org.spongepowered.plugin' version '0.8.1'
}
group = pluginGroup
version = pluginVersion
dependencies {
implementation 'org.spongepowered:spongeapi:7.1.0-SNAPSHOT'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
}
sponge.plugin.id = pluginId
That’s how my main plugin class looks like (removed @Plugin)
public class Core {
@Inject
private Logger logger;
@Listener
public void onServerStart(GameStartedServerEvent event) {
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://localhost:3000/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
APICaller apiCaller = retrofit.create(APICaller.class);
Call<List<User>> call = apiCaller.users();
call.enqueue(new Callback<List<User>>() {
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
logger.info("async request done");
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
logger.info("async request failed");
}
});
}
}
User.java
public class User {
private int id;
private String name;
public String getName() {
return this.name;
}
}
APICaller interface:
public interface APICaller {
@GET("user/")
Call<List<User>> users();
}
So what did I do wrong? I hope I didn’t miss anything. If I did, please tell me
Thanks guys!
Edit
The error occurs, because gradle does not bundle dependencies (by default). I managed to get it working by using following gradle plugin: GitHub - johnrengelman/shadow: Gradle plugin to create fat/uber JARs, apply file transforms, and relocate packages for applications and libraries. Gradle version of Maven's Shade plugin.