2012-02-09 97 views
0

下面的代碼:如果評論我沒有得到異常異常,而試圖用球衣和JAXB

javax.servlet.ServletException: Servlet.init() for servlet Jersey Rest Service threw exception 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394) 
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) 
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    java.lang.Thread.run(Thread.java:662) 

root cause 

com.sun.jersey.spi.inject.Errors$ErrorMessagesException 
    com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170) 
    com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136) 
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199) 
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771) 
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766) 
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488) 
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318) 
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609) 
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210) 
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373) 
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556) 
    javax.servlet.GenericServlet.init(GenericServlet.java:160) 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394) 
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) 
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    java.lang.Thread.run(Thread.java:662) 

@POST 
@Path("/previous-status/{current}") 
@Consumes(MediaType.APPLICATION_XML) 
@Produces(MediaType.TEXT_PLAIN) 
public String getPreviousStepStatus(@PathParam("current") JAXBElement<WorkflowStep> step) { 
    WorkflowStep wfStep = step.getValue(); 
    return DBAccessor.getPrevStepStatus(wfStep); 
} 

產生以下例外。在Web應用程序的當前使用的庫是:

asm-3.1.jar 
jersey-core-1.11.jar 
jersey-server-1.11.jar 
jsr311-api-1.1.1.jar 
jersey-servlet-1.11.jar 
activation.jar (part of jaxb distribution) 
jaxb-api.jar 
jaxb-impl.jar 
stax-api-1.0.1.jar 
woodstox-core-asl-4.1.2.jar 

我已經包括在列表中的最後5個庫的原因是這篇文章從developer works

而且從Tomcat啓動日誌我看到以下內容:

SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
    SEVERE: Missing dependency for method public java.lang.String restful.SilverLine.getPreviousStepStatus(javax.xml.bind.JAXBElement) at parameter at index 0 
    SEVERE: Method, public java.lang.String restful.SilverLine.getPreviousStepStatus(javax.xml.bind.JAXBElement), annotated with POST of resource, class restful.SilverLine, is not recognized as valid resource method. 

任何想法將不勝感激?

回答

1

PathParameter和實體是不同的東西,嘗試以下操作:

@POST 
@Path("/previous-status/{order}") 
@Consumes(MediaType.APPLICATION_XML) 
@Produces(MediaType.TEXT_PLAIN) 
public String getPreviousStepStatus(@PathParam("order") int order, JAXBElement<WorkflowStep> step) { 

    ... 

    WorkflowStep wfStep = step.getValue(); 
    return DBAccessor.getPrevStepStatus(wfStep); 
} 

在這種情況下,你可以這樣做POST請求http://host/previous-status/10,把任何你想要進入實體。

你在路徑參數(它是URL的一部分)中有xml,這是不可能使用的。您的請求看起來像POST http://host/previous-status/<some-xml><foo>bar</foo></some-xml >,這是..不好主意,不支持。

您應該看看Jersey用戶指南:http://jersey.java.net/nonav/documentation/latest/,其中包括路徑參數注入,訪問實體和使用XML等主題。