Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassNotFoundException for registered response writer #62

Closed
kevinstier opened this issue May 29, 2020 · 6 comments
Closed

ClassNotFoundException for registered response writer #62

kevinstier opened this issue May 29, 2020 · 6 comments

Comments

@kevinstier
Copy link

I have the following response writer:

@Produces("application/json")
public class DefaultResponseWriter<T> implements HttpResponseWriter<T> {

    @Override
    public void write(T result, HttpServerRequest request, HttpServerResponse response) {

        if (result != null) {
            response.end(JsonUtils.toJson(result, DatabindCodec.mapper()));
        }
        else {
            response.end();
        }
    }
}

Which is basically the same as the provided JsonResponseWriter, except for the @Produces("application/json"). When I register this writer as

RestRouter.getWriters().register(DefaultResponseWriter.class);

I receive the following exception:

java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
	at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:130)
	at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:97)
	at javax.ws.rs.core.MediaType.toString(MediaType.java:395)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at com.zandero.rest.writer.WriterFactory.register(WriterFactory.java:158)
	at com.zandero.rest.writer.WriterFactory.register(WriterFactory.java:96)
	at demo.rest.Api.start(Api.java:173)
	at io.vertx.core.AbstractVerticle.start(AbstractVerticle.java:106)
	at io.vertx.rxjava.core.AbstractVerticle.start(AbstractVerticle.java:43)
	at io.vertx.core.Verticle.start(Verticle.java:66)
	at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$8(DeploymentManager.java:556)
	at io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:369)
	at io.vertx.core.impl.EventLoopContext.lambda$executeAsync$0(EventLoopContext.java:38)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:163)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:416)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:515)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:87)
	at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:185)
	at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:112)
	... 21 more
@drejc
Copy link
Member

drejc commented May 29, 2020

As far as I can tell from the provided stack trace ... it seems like the registered writer has no media type assigned an it fails on a log statement:

at javax.ws.rs.core.MediaType.toString(MediaType.java:395)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at com.zandero.rest.writer.WriterFactory.register(WriterFactory.java:158)
	at com.zandero.rest.writer.WriterFactory.register(WriterFactory.java:96)

Nevertheless I have tried to replicate this on my own as I see no problem with the response writer implementation and as usual it works on my machine.

So something must be problematic with the setup / or maybe a third library ... hard to tell.

Can you create a breakpoint at: com.zandero.rest.writer.WriterFactory.register(WriterFactory.java:96)
and check the values of type and writer variables?

@kevinstier
Copy link
Author

The value of writer is Class demo.DefaultResponseWriter. The value of type is a MediaType with type "application", subtype "json" and paramaters is empty


@drejc
Copy link
Member

drejc commented May 29, 2020 via email

@kevinstier
Copy link
Author

The log in the register method of WriterFactory prints MediaType in a String. This causes a call to toString() on MediaType. This method tries to get an instance for javax.ws.rs.ext.RuntimeDelegate with fallback class org.glassfish.jersey.internal.RuntimeDelegateImpl, but is unable to find both.

The same exception is thrown when I register the writer as:
RestRouter.getWriters().register(MediaType.APPLICATION_JSON_TYPE, DefaultResponseWriter.class);

Do you have an idea what can cause this problem?

@drejc
Copy link
Member

drejc commented May 29, 2020

Jap ... this is what I was assuming from the first stack trace ...

In my local project I see a reference to: org.glassfish.jersey.core:jersey-common:2.22.2
and I have the following test dependecy:

<dependency>
           <groupId>org.glassfish.jersey.core</groupId>
           <artifactId>jersey-common</artifactId>
           <version>${version.glassfish}</version>
           <scope>test</scope>
       </dependency>

I assume this is the problem.

I will fix this so the project will not be dependent on this library (it probably is not needed). In the mean time if you add this dependency it will probably work.

@kevinstier
Copy link
Author

This solved the problem! Thank you

drejc added a commit that referenced this issue Jun 3, 2020
….jar implementation present in final code if not needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants