2016-03-20 77 views
0

丟失的依賴我有這樣的代碼:的方法public void

@Path("/automation/devices") 
@Singleton 
public class DevicesWebService { 
    private AppiumServerManager appiumServerManager; 
    private AdbService adbService; 
    private DevicesService deviceService; 

    public DevicesWebService() { 
     this(new AdbServiceLocal(), new AppiumServerManagerLocal()); 
    } 

    @VisibleForTesting 
    public DevicesWebService(AdbService adbService, AppiumServerManagerLocal AppiumServerManager) { 
     this.adbService = adbService; 
     this.appiumServerManager = AppiumServerManager; 
    } 


    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage() { 
     return "welocme to devices web-service"; 
    } 

    @POST 
    @Consumes({MediaType.APPLICATION_JSON}) 
    @Produces({MediaType.TEXT_PLAIN}) 
    @Path("/acquireDevice/{device}/{port}") 
    public Response acquireDevice(@PathParam("device") Device device, @PathParam("port") Integer port) throws 
      Exception { 
     try { 
      deviceService.acquireDevice(device, port); 
     } catch (RuntimeException e) { 
      return Response.status(Response.Status.NOT_FOUND).build(); 
     } 
     return Response.status(Response.Status.OK).build(); 

    } 
// 
    public static void main(String[] args) throws IOException { 
     HttpServer server = HttpServerFactory.create("http://localhost:9998/"); 
     server.start(); 
     System.out.println("Server running"); 
     System.out.println("Visit: http://localhost:9998/automation/devices"); 
    } 
} 

我的代碼在運行時失敗與此異常:

Mar 20, 2016 12:37:51 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses 
INFO: Root resource classes found: 
    class com.m.mobileautomation.devices.webServices.DevicesWebService 
    class com.m.mobileautomation.MobileAutomationWebService 
Mar 20, 2016 12:37:51 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses 
INFO: Provider classes found: 
    class org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider 
    class org.codehaus.jackson.jaxrs.JacksonJsonProvider 
    class org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper 
    class org.codehaus.jackson.jaxrs.JsonParseExceptionMapper 
Mar 20, 2016 12:37:51 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate 
INFO: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 05:39 AM' 
Mar 20, 2016 12:37:52 PM com.sun.jersey.spi.inject.Errors processErrorMessages 
SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
    SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.m.mobileautomation.devices.webServices.DevicesWebService.acquireDevice(com.m.mobileautomation.devices.dataModel.Device,java.lang.Integer) throws java.lang.Exception at parameter at index 0 
    SEVERE: Method, public javax.ws.rs.core.Response com.m.mobileautomation.devices.webServices.DevicesWebService.acquireDevice(com.m.mobileautomation.devices.dataModel.Device,java.lang.Integer) throws java.lang.Exception, annotated with POST of resource, class com.m.mobileautomation.devices.webServices.DevicesWebService, is not recognized as valid resource method. 
Exception in thread "main" 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.api.container.ContainerFactory.createContainer(ContainerFactory.java:172) 
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:264) 
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:246) 
    at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117) 
    at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92) 
    at com.m.mobileautomation.devices.webServices.DevicesWebService.main(DevicesWebService.java:86) 
    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:483) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

如果我註釋掉public Response acquireDevice一切正常。

我曾試圖刪除``很好,但還是得到了一個錯誤:

SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
    SEVERE: Missing dependency for method public void com.m.mobileautomation.devices.webServices.DevicesWebService.acquireDevice(com.m.mobileautomation.devices.dataModel.Device,java.lang.Integer) throws java.lang.Exception at parameter at index 0 
    SEVERE: Method, public void com.m.mobileautomation.devices.webServices.DevicesWebService.acquireDevice(com.m.mobileautomation.devices.dataModel.Device,java.lang.Integer) throws java.lang.Exception, annotated with POST of resource, class com.m.mobileautomation.devices.webServices.DevicesWebService, is not recognized as valid resource method. 
Exception in thread "main" 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.api.container.ContainerFactory.createContainer(ContainerFactory.java:172) 
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:264) 
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:246) 
    at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117) 
    at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92) 
    at com.waze.mobileautomation.devices.webServices.DevicesWebService.main(DevicesWebService.java:87) 
    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:483) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

我該如何解決這個問題?

+0

閱讀Javadoc的'@ PathParam',它會告訴你使用自定義類型參數的要求。 Jersey不知道如何從字符串中創建'Device'。但是有幾種方法可以讓澤西島知道如何創造它。請參閱javadoc –

+0

@PathParam java doc僅處理params,不命名從字符串到java對象的轉換。我跟着這個教程,我錯過了什麼? http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ –

+0

你沒看過關於靜態'fromString(String)'或'valueOf(String)'的東西嗎?方法或接受字符串的構造函數?這些是你的'Device'類的選項,如果你想爲'@ PathParam'使用它的參數類型。你需要從傳遞給這三個選項中的任何一個的字符串構造'Device' –

回答

2

正如@peeskillet所說,@PathParam不應該用於傳遞對象,因爲它更常用於指定一個變量,用於標識您操作的資源/對象(例如在您的情況下,我可能會使用acquireDevice/{deviceId}/{port}如果說,這個id事先有任何意義)。

像你現在這樣使用它,當你測試或者需要查看日誌中發生了什麼時,也可以真正地混淆你的東西 - 想想如何在你的應用程序中看到一箇中間夾有json的條目訪問日誌。

如果你正在尋找傳遞對象,你應該肯定使用請求的身體 - 也注意到傑克遜(我看到你標記了它,所以我假設這是你使用澤西島)必須知道如何反序列化該對象好,否則你也會失敗。

相關問題