Narayan
Narayan

Reputation: 169

Dropwizard not accepting @PathParam for pojo objects

I am using dropwizard for micro service . I want to pass a object at the receiving end .

@GET
@Path("/run")
public String runReport( @PathParam(value = "report") Report report){
    return "Report Service is running: Status good";
}

Here Report is a simple pojo defined as

@JsonIgnoreProperties(ignoreUnknown = true)
public class Report  {

private static final long serialVersionUID = -558913649L;

/** hashCode temporary storage. */
private volatile Integer hashCode;

/** Field mapping. */
private String description;
}

I am using the dropwizard version 0.7.1

but when I try to run the program from eclipse . it gives me the error.Do i need to add some external jar here . I presumed drop-wizard does everything. And when I change the PathParam from report to a simple long it runs ok

ERROR [2014-10-27 18:24:54,792] com.sun.jersey.spi.inject.Errors: The following errors and                 
warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public java.lang.String    
com.sdata.report.resources.ReportResource.runReport(com.sdata.report.resources.Report) at   
parameter at index 0
Exception in thread "main" javax.servlet.ServletException:   
com.sun.jersey.spi.container.servlet.ServletContainer- 
68792330@565a8b6c==com.sun.jersey.spi.container.servlet.ServletContainer,1,false
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:561)
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:349)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:812)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:288)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:732)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:100)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at com.codahale.metrics.jetty9.InstrumentedHandler.doStart(InstrumentedHandler.java:92)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:100)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:100)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.server.handler.RequestLogHandler.doStart(RequestLogHandler.java:131)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:100)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:233)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
at org.eclipse.jetty.server.Server.start(Server.java:342)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:100)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.server.Server.doStart(Server.java:290)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:43)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76)
at io.dropwizard.cli.Cli.run(Cli.java:70)
at io.dropwizard.Application.run(Application.java:72)
at com.sdata.report.ReportApplication.main(ReportApplication.java:12)
Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
at    
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
at 
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:491)
at 
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:540)
... 36 more
WARN  [2014-10-27 18:24:54,795] /: unavailable

Upvotes: 2

Views: 1286

Answers (1)

David Spence
David Spence

Reputation: 8079

Back to basics. A query parameter is an item after a question mark in a url, separated by the "&" symbol e.g.

yourwebsite.com/run?report=1&reportName=dogs

Here are two query params, report and reportName. You should never have to send a complex object in a GET request (what happens if you just paste this into your address bar). As you can probably tell, those query params are easy to parse as long and string. So no complex objects as query params.

A path parameter is an item within the url e.g.

yourwebsite.com/run/1

Here there is one path param, the "1". It's a path param because it's part of the actual path, it's not extra information provided like in query params (which are usually optional).

You should send a complex object (like your report) in a request like POST or PUT where you are telling the server to update something and providing the multiple values.

So, you've explained that you would like to pass an object to the receiving end. To do this, you should best change the @GET annotation to @POST, the path can stay the same. But you will not include your complex object as a query param. Instead, it should be posted as json. Dropwizard should do the deserialization, providing the property names match up. So your endpoint can look like so:

@POST
@Path("/run")
public String runReport(Report report) {
    //
}

An example request to this would be to perform a POST to yourwebsite.com/run with json like:

{
    serialVersionUID: 1,
    hashCode: 1,
    description: "xxx"
}

Also note, the properties in your Report class should be annotated with @JsonProperty to be deserialized. If anything isn't clear, leave a comment and I can try to help.

Upvotes: 3

Related Questions