unable to render as JSON

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

unable to render as JSON

vsajja
Apologies for the dumb question.
I'm simply trying  to render some JSON and am running into a NoSuchRendererException.

-- ratpack.groovy --

import static ratpack.groovy.Groovy.ratpack
import static ratpack.jackson.Jackson.json
import ratpack.jackson.JacksonModule

ratpack {
  bindings {
    new JacksonModule()
  }
  handlers {
    get("person") {
      render json(new Person("John"))
    }
    assets "dist"
  }
}

class Person {
  private final String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}

-- stacktrace --

main] INFO ratpack.server.internal.NettyRatpackServer - Ratpack started for http://localhost:8080
ratpack-compute-1-2] ERROR ratpack.error.internal.DefaultDevelopmentErrorHandler - exception thrown for request to /person?
atpack.render.NoSuchRendererException: No renderer for object '[email protected]' of type 'class ratpack.jackson.internal.DefaultJsonRender'
       at ratpack.render.internal.DefaultRenderController.lambda$render$91(DefaultRenderController.java:40)
       at ratpack.render.internal.DefaultRenderController$$Lambda$26/327736199.get(Unknown Source)
       at java.util.Optional.orElseThrow(Optional.java:290)
       at ratpack.render.internal.DefaultRenderController.render(DefaultRenderController.java:40)
       at ratpack.handling.internal.DefaultContext.render(DefaultContext.java:289)
       at ratpack.groovy.handling.internal.DefaultGroovyContext.render(DefaultGroovyContext.java:191)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:497)
       at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
       at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
       at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:368)
       at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
       at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
       at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
       at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
       at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
       at ratpack$_run_closure1_closure3_closure4.doCall(ratpack.groovy:11)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:497)
       at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
       at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
       at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:278)
       at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
       at groovy.lang.Closure.call(Closure.java:423)
       at groovy.lang.Closure.call(Closure.java:439)
       at ratpack.groovy.internal.ClosureInvoker.invoke(ClosureInvoker.java:59)
       at ratpack.groovy.handling.internal.ClosureBackedHandler.handle(ClosureBackedHandler.java:41)
       at ratpack.handling.internal.DefaultContext.next(DefaultContext.java:239)
       at ratpack.http.internal.MethodHandler.handle(MethodHandler.java:41)
       at ratpack.handling.internal.DefaultContext.next(DefaultContext.java:239)
       at ratpack.handling.internal.DefaultContext.insert(DefaultContext.java:270)
       at ratpack.path.internal.PathHandler.handle(PathHandler.java:38)
       at ratpack.handling.internal.DefaultContext.next(DefaultContext.java:239)
       at ratpack.handling.internal.DefaultContext.next(DefaultContext.java:252)
       at ratpack.handling.internal.RegistryNextHandler.handle(RegistryNextHandler.java:33)
       at ratpack.handling.internal.DefaultContext.next(DefaultContext.java:239)
       at ratpack.handling.internal.DefaultContext.insert(DefaultContext.java:261)
       at ratpack.handling.internal.ChainHandler.handle(ChainHandler.java:45)
       at ratpack.groovy.internal.ScriptBackedApp.handle(ScriptBackedApp.java:84)
       at ratpack.handling.internal.DefaultContext.next(DefaultContext.java:239)
       at ratpack.handling.internal.DefaultContext.lambda$start$73(DefaultContext.java:149)
       at ratpack.handling.inDefaultContext$$Lambda$20/1046893936.execute(Unknown Source)
       at ratpack.exec.internal.ExecutionBacking$1.performOperation(ExecutionBacking.java:264)
       at ratpack.exec.internal.InterceptedOperation.run(InterceptedOperation.java:43)
       at ratpack.exec.internal.ExecutionBacking.intercept(ExecutionBacking.java:266)
       at ratpack.exec.internal.ExecutionBacking$UserCodeSegment.run(ExecutionBacking.java:308)
       at ratpack.exec.internal.ExecutionBacking.drain(ExecutionBacking.java:220)
       at ratpack.exec.internal.ExecutionBacking.<init>(ExecutionBacking.java:89)
       at ratpack.exec.internal.DefaultExecControl$1.start(DefaultExecControl.java:130)
       at ratpack.handling.internal.DefaultContext.start(DefaultContext.java:149)
       at ratpack.server.internal.NettyHandlerAdapter.channelRead0(NettyHandlerAdapter.java:171)
       at ratpack.server.internal.NettyHandlerAdapter.channelRead0(NettyHandlerAdapter.java:78)
       at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
       at ratpack.server.internal.NettyHandlerAdapter.channelRead(NettyHandlerAdapter.java:131)
       at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
       at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
       at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
       at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
       at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
       at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
       at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
       at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
       at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)
       atty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
       at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
       at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)
       at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
       at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
       at ratpack.exec.internal.DefaultExecController$ExecControllerBindingThreadFactory.lambda$newThread$54(DefaultExecController.java:84)
       at ratpack.exec.internal.DefaultExecController$ExecControllerBindingThreadFactory$$Lambda$3/240166646.run(Unknown Source)
       at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
       at java.lang.Thread.run(Thread.java:745)

-- build.gradle --

buildscript {
  repositories {
    maven { url 'http://oss.jfrog.org/repo' }
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath 'io.ratpack:ratpack-gradle:1.0.0-rc-3'
  }
}

apply plugin: 'io.ratpack.ratpack-groovy'

repositories {
  maven { url 'http://oss.jfrog.org/repo' }
  mavenCentral()
  jcenter()
}

dependencies {
//  compile 'io.ratpack:ratpack-jackson:1.0.0-rc-3'
  compile ratpack.dependency("jackson")
  compile 'org.slf4j:slf4j-simple:1.7.12'
}

run {
  jvmArgs "-Dratpack.port=8080"
}
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

danhyun
Which version of Ratpack are you using? In the latest version there is no need to add a JacksonModule but in your listing you need to add it by invoking the "bind" method. i.e.
bindings {
  bind new JacksonModule()
}
Latest version of Ratpack however has the jackson stuff baked into core so now you can do this:
@Grab('io.ratpack:ratpack-groovy:1.0.0-rc-2')

import static ratpack.groovy.Groovy.ratpack
import static ratpack.jackson.Jackson.json

ratpack {
  handlers {
    get("person") {
      render json(new Person("John"))
    }
  }
}

class Person {
  private final String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

vsajja
Thanks for the quick response.
I'm using the latest version of Ratpack, 1.0.0-rc-3, but still struggling with this (this time with server startup).

"Error: Could not find or load main class ratpack.groovy.launch.GroovyRatpackMain".

-- build.gradle --

buildscript {
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath 'io.ratpack:ratpack-gradle:1.0.0-rc-3'
  }
}

apply plugin: "io.ratpack.ratpack-groovy"

repositories {
  mavenCentral()
  jcenter()
}

dependencies {
  compile 'io.ratpack:ratpack-groovy:1.0.0-rc-3'
  runtime 'org.slf4j:slf4j-simple:1.7.12'
}

-- ratpack.groovy -- (removed the JacksonModule binding as you suggested)

import static ratpack.groovy.Groovy.ratpack
import static ratpack.jackson.Jackson.json

ratpack {
  handlers {
    get("person") {
      render json(new Person("John"))
    }
  }
}

class Person {
  private final String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}

Any help much appreciated.

Thanks,
Vinod
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

danhyun
What does your directory structure look like for your project?
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

vsajja
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

danhyun
I should have also asked, how are you trying to run your application?
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

vsajja
Using git bash for Windows. Under the webconsole directory, the run task..
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

vsajja
FYI: If I exclude webconsole from the root project, everything works.

http://i.imgur.com/yteXXUE.png

-- my settings.gradle --
rootProject.name = 'vsajja'

//include 'webconsole'

Cheers,
Vinod
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

danhyun
In reply to this post by vsajja
I'm using git bash with the same directory structure, I've simplified to just

build.gradle

and

src/ratpack/ratpack.groovy

and it worked with your contents.

Perhaps try running `./gradlew clean run -i`.

After confirming that it works unchanged you should know that the line containing

 compile 'io.ratpack:ratpack-groovy:1.0.0-rc-3'

Is unnecessary because of the ratpack gradle groovy plugin that is being applied.
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

danhyun
In reply to this post by vsajja
Ok if you include webconsole again then run `./gradlew clean webconsole:run -i` what do you get?
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

vsajja
In reply to this post by danhyun
./gradlew clean run -i`.

The clean run worked, thanks. I'm able to startup.

Without the  compile 'io.ratpack:ratpack-groovy:1.0.0-rc-3' I'm running into compile errors.

ratpack.groovy: 2: unable to resolve class ratpack.jackson.Jackson
 @ line 2, column 1.
   import static ratpack.jackson.Jackson.json
   ^

1 error
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

danhyun
That's interesting, I think this is more of a project organization problem than a Ratpack problem. I didn't first catch that you were using a Gradle subproject approach to this.

At any rate here is my build.gradle file that compiles and runs the project for me


buildscript {
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath 'io.ratpack:ratpack-gradle:1.0.0-rc-3'
  }
}

apply plugin: "io.ratpack.ratpack-groovy"

repositories {
  mavenCentral()
  jcenter()
}

dependencies {
  runtime 'org.slf4j:slf4j-simple:1.7.12'
}
Reply | Threaded
Open this post in threaded view
|

Re: unable to render as JSON

vsajja
Thank you for your help. All is well.