Can't figure out how to return a custom class as a json string. Here is
what I was trying but when I hit:
http://localhost:8080/example/person/4
I get:
No message body writer has been found for response class Person.
#!/usr/bin/env groovy
@Grab(group="org.apache.camel", module="camel-core", version="2.8.0")
@Grab(group="org.apache.camel", module="camel-jetty", version="2.8.0")
@Grab(group="org.apache.camel", module="camel-cxf", version="2.8.0")
@Grab(group='ch.qos.logback', module='logback-classic', version='0.9.29')
import org.apache.camel.CamelContext
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.impl.SimpleRegistry
import org.apache.camel.builder.RouteBuilder
import javax.ws.rs.*
class Person {
Integer id
String name
}
@Path("/example")
class MyExampleResource{
@GET
@Produces("application/json")
@Path("person/{id}")
public Person getPerson(@PathParam("id") Integer id){
Person p = new Person()
p.id = id
p.name = "Bob"
return p
}
}
CamelContext cxt = new DefaultCamelContext()
cxt.registry = new SimpleRegistry()
cxt.registry.registry.put("myResources", [new MyExampleResource()])
cxt.registry.registry.put("jsonProvider", new
org.apache.cxf.jaxrs.provider.JSONProvider())
cxt.addRoutes(new RouteBuilder(){
void configure(){
from("jetty:http://localhost:8080?matchOnUriPrefix=true")
.to("cxfbean:myResources?providers=#jsonProvider")
}
})
cxt.start()