Dropwizard不接受pojo对象的@PathParam

时间:2014-10-27 18:31:36

标签: java dropwizard

我正在使用dropwizard进行微服务。我想在接收端传递一个对象。

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

此报告是一个简单的pojo,定义为

@JsonIgnoreProperties(ignoreUnknown = true)
public class Report  {

private static final long serialVersionUID = -558913649L;

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

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

我正在使用dropwizard版本0.7.1

但是当我尝试从eclipse运行程序时。它给了我错误。我需要在这里添加一些外部jar。我认为drop-wizard可以做任何事情。当我将PathParam从报告更改为简单的long时,它运行正常

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

1 个答案:

答案 0 :(得分:3)

回到基础。 查询参数是网址中问号后面的项目,由"&"分隔。符号,例如。

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

以下是两个查询参数reportreportName。您永远不必在GET请求中发送复杂对象(如果您只是将其粘贴到地址栏中会发生什么)。正如您可能知道的那样,那些查询参数很容易解析为long和string。所以没有复杂的对象作为查询参数。

路径参数是网址中的项目,例如

yourwebsite.com/run/1

这里有一个路径参数," 1"。它是一个路径参数,因为它是实际路径的一部分,它不是像查询参数(通常是可选的)那样提供的额外信息。

您应该在POST或PUT等请求中发送一个复杂对象(如报表),告诉服务器更新某些内容并提供多个值。

因此,您已经解释过您希望将对象传递给接收端。为此,您最好将@GET注释更改为@POST,路径可以保持不变。但是您不会将复杂对象包含为查询参数。相反,它应该发布为json。 Dropwizard应该进行反序列化,提供属性名称匹配。所以你的端点看起来像这样:

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

对此的一个示例请求是使用json执行POSTyourwebsite.com/run

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

另请注意,Report类中的属性应使用@JsonProperty进行注释以进行反序列化。如果有什么不清楚,请发表评论,我可以尝试提供帮助。