2017-02-14 55 views
0

我想讓Twilio在Dropwizard應用程序中工作。該程序編譯,但我在引導服務器時不斷得到以下失敗。Twilio/Dropwizard運行時錯誤

$ sudo ./startpiservice.sh 
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/type/ReferenceType at com.fasterxml.jackson.datatype.guava.GuavaModule.setupModule(GuavaModule.java:55) at com.fasterxml.jackson.databind.ObjectMapper.registerModule(ObjectMapper.java:550) at io.dropwizard.jackson.Jackson.configure(Jackson.java:54) at io.dropwizard.jackson.Jackson.newObjectMapper(Jackson.java:24) at io.dropwizard.setup.Bootstrap.<init>(Bootstrap.java:64) at io.dropwizard.Application.run(Application.java:72) at applicationconfiguration.HomeApplication.main(HomeApplication.java:71) Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.type.ReferenceType at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more 

整個文件可以在這裏找到我的Dropbox的...對不起,我在我的日常工作,而不能訪問github上上傳這裏面。

https://www.dropbox.com/sh/or5v14uyyjplvmr/AAB4uoHdRDYLZVylUfGNotkfa?dl=0

有問題的類是在SMSResource.java \ pirestservice的\ src \主\ java的\資源\ SMSResource.java

代碼編譯並運行沒有任何代碼語句和進口的Twilio,但是一旦我使用導入語句和代碼語句,程序在引導期間就會失敗。

我是相對較新的使用Maven和Dropwizard ......這是一個問題與依賴衝突?我錯過了什麼嗎? Twilio代碼工作在一個簡單的獨立程序中,但不在Dropwizard中。

~~~~~~~~~~~~~~~~~~~~~~ UPDATE 2017年3月2日~~~~~~~~~~~~~~~~~

根據hortega的建議,我修改了我的Maven POM文件並編譯了程序,並在部署時運行了服務器。所有的方法調用工作,除了我需要的是「/ sendms」。這是POM文件,java類文件和堆棧跟蹤的一部分。

<!-- Twilio --> 
<dependency> 
    <groupId>com.twilio.sdk</groupId> 
    <artifactId>twilio</artifactId> 
    <version>(7.0,7.9)</version> 
    <exclusions> 
     <exclusion> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-annotations</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 

package resources; 

import java.util.List; 
import java.util.Timer; 
import java.util.TimerTask; 
import javax.annotation.security.RolesAllowed; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import com.twilio.Twilio; 
import com.twilio.rest.api.v2010.account.Message; 
import com.twilio.type.PhoneNumber; 

import authentication.User; 
import database.AccountDAO; 
import database.UserDAO; 
import io.dropwizard.hibernate.UnitOfWork; 


/** 
* This class defines the URLs and provides the interface to HTTP requests and works 
* with the class PiGpioService to set and retrieve information from the model. 
*/ 
@Path("/smsnotification") 
@RolesAllowed("ADMIN") 
@Produces(MediaType.APPLICATION_XML) 
@Consumes(MediaType.APPLICATION_XML) 
public class SMSResource implements GpioObserver 
{ 
    private static final Logger LOGGER = LoggerFactory.getLogger(SMSResource.class); 
    private final AccountDAO accountDAO; 
    private final UserDAO userDAO; 
    private int count; 
    private Timer timer; 
    private boolean timerSet = false; 

    public SMSResource(AccountDAO accountDAO, UserDAO userDAO) 
    { 
     this.accountDAO = accountDAO; 
     this.userDAO = userDAO; 
    } 

    /** 
    * method called by GpioRepresentation class when there is a 
    * change in state on garage door. 
    * @param String state 
    */ 
    public void update(String state) 
    { 
     //if state is HIGH trigger a timer to start. Kill the timer if 
     //the state is low. 
     if(state.equals("HIGH")) 
     { 
      //conditional to prevent multiple Timer objects 
      if(!timerSet) 
      { 
       timer = new Timer(); 
       int delay = (20 * 60 * 1000); 
       int period = (20 * 60 * 1000); 

       //schedule first message to be sent when door is open 20 minutes. 
       //then send a message every 20 minutes to a maximum of 
       //3 times. 
       MessageExecutor msgExec = new MessageExecutor(); 
       timer.schedule(msgExec, delay, period); 
       timerSet = true; 
      } 
     } 
     else 
     { 
      //cancel timer if the garage door is closed and reset the count 
      timer.cancel(); 
      count = 0; 
      timerSet = false; 
     } 
    } 

    /** 
    * Send an SMS text message. 
    * FOR TESTING ONLY 
    */ 
    @GET 
    @Path("/sendsms") 
    @UnitOfWork 
    public Response sendSMSMessage() 
    { 
     List<User> users; 
     List<TwilioAccount> accountDetails; 
     int index = 0; 

     users = userDAO.getAllUsers(); 
     accountDetails = accountDAO.getAccountDetails(); 

     Twilio.init(accountDetails.get(index).getSID(), accountDetails.get(index).getToken()); 

     for(User u : users) 
     { 
      LOGGER.info("SendSMS URL Works and I can get my USER and TWILIO Details," 
        + "User {}, phone number {}, SID = {}, token = {}, from# = {}.", 
        u.getName(), u.getPhoneNumber(), accountDetails.get(index).getSID(), accountDetails.get(index).getToken(), 
        accountDetails.get(index).getPhoneNumber()); 

      Message message = Message 
        .creator(new PhoneNumber(u.getPhoneNumber()), new PhoneNumber(accountDetails.get(index).getPhoneNumber()), 
         "This is the ship that made the Kessel Run in fourteen parsecs?") 
        .setMediaUrl("https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg") 
        .create(); 

     } 

     return Response.ok("message(s) sent").build(); 
    } 

    /** 
    * This private (inner) class is responsible for initiating a message 
    * being sent every 20 minutes to alert that the garage door has been 
    * left open. 
    */ 
    private class MessageExecutor extends TimerTask 
    { 
     @Override 
     @UnitOfWork 
     public void run() 
     { 

      if(count < 3) 
      { 
       List<User> users; 
       List<TwilioAccount> accountDetails; 
       int index = 0; 

       users = userDAO.getAllUsers(); 
       accountDetails = accountDAO.getAccountDetails(); 

       //Twilio.init(accountDetails.get(index).getSID(), accountDetails.get(index).getToken()); 

       for(User u : users) 
       { 
        LOGGER.info("SendSMS URL Works and I can get my USER and TWILIO Details," 
          + "User {}, phone number {}, SID = {}, token = {}, from# = {}.", 
          u.getName(), u.getPhoneNumber(), accountDetails.get(index).getSID(), accountDetails.get(index).getToken(), 
          accountDetails.get(index).getPhoneNumber()); 

        //Message message = Message 
         //  .creator(new PhoneNumber(u.getPhoneNumber()), new PhoneNumber(accountDetails.get(index).getPhoneNumber()), 
         //  "This is the ship that made the Kessel Run in fourteen parsecs?") 
          // .setMediaUrl("https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg") 
          //.create(); 

       } 
       count++; 
      } 
     } 
    } 
} 

這裏是堆棧跟蹤/記錄器輸出:

INFO [2017-03-03 02:14:29,347] authentication.BasicAuthenticator: User: Mike logged in with email: [email protected] and password: xxxxxx. 
INFO [2017-03-03 02:14:29,374] org.hibernate.engine.internal.StatisticalLoggingSessionEventListener: Session Metrics { 
    531871 nanoseconds spent acquiring 1 JDBC connections; 
    0 nanoseconds spent releasing 0 JDBC connections; 
    5236262 nanoseconds spent preparing 1 JDBC statements; 
    6003028 nanoseconds spent executing 1 JDBC statements; 
    0 nanoseconds spent executing 0 JDBC batches; 
    0 nanoseconds spent performing 0 L2C puts; 
    0 nanoseconds spent performing 0 L2C hits; 
    0 nanoseconds spent performing 0 L2C misses; 
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections); 
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections) 
} 
INFO [2017-03-03 02:14:29,571] resources.SMSResource: SendSMS URL Works and I can get my USER and TWILIO Details,User Mike, phone number +1xxxxxxxxxx, SID = ACxxxxxxxxxxxxx, token = 1xxxxxxxxxx5, from# = +1xxx5xx8xx9. 
INFO [2017-03-03 02:14:37,092] org.hibernate.engine.internal.StatisticalLoggingSessionEventListener: Session Metrics { 
    526871 nanoseconds spent acquiring 1 JDBC connections; 
    0 nanoseconds spent releasing 0 JDBC connections; 
    5925736 nanoseconds spent preparing 2 JDBC statements; 
    6532189 nanoseconds spent executing 2 JDBC statements; 
    0 nanoseconds spent executing 0 JDBC batches; 
    0 nanoseconds spent performing 0 L2C puts; 
    0 nanoseconds spent performing 0 L2C hits; 
    0 nanoseconds spent performing 0 L2C misses; 
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections); 
    29239673 nanoseconds spent executing 2 partial-flushes (flushing a total of 2 entities and 2 collections) 
} 
ERROR [2017-03-03 02:14:37,113] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: 0cd82e2d769a36c4 
! com.twilio.exception.ApiException: java.lang.IllegalStateException 
! at com.twilio.http.NetworkHttpClient.makeRequest(NetworkHttpClient.java:89) 
! at com.twilio.http.HttpClient.reliableRequest(HttpClient.java:38) 
! at com.twilio.http.HttpClient.reliableRequest(HttpClient.java:22) 
! at com.twilio.http.TwilioRestClient.request(TwilioRestClient.java:42) 
! at com.twilio.rest.api.v2010.account.MessageCreator.create(MessageCreator.java:307) 
! at com.twilio.rest.api.v2010.account.MessageCreator.create(MessageCreator.java:25) 
! at com.twilio.base.Creator.create(Creator.java:45) 
! at resources.SMSResource.sendSMSMessage(SMSResource.java:139) 
! 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:498) 
! at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) 
! at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144) 
! at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) 
! at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160) 
! at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) 
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) 
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) 
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) 
! at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326) 
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) 
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) 
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315) 
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297) 
! at org.glassfish.jersey.internal.Errors.process(Errors.java:267) 
! at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) 
! at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) 
! at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) 
! at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) 
! at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) 
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) 
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) 
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) 
! at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49) 
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1689) 
! at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:34) 
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) 
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:50) 
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:44) 
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) 
! at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581) 
! at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1174) 
! at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511) 
! at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1106) 
! at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) 
! at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:240) 
! at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51) 
! at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:459) 
! at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:68) 
! at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:56) 
! at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:169) 
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) 
! at org.eclipse.jetty.server.Server.handle(Server.java:524) 
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319) 
! at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253) 
! at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273) 
! at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) 
! at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) 
! at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) 
! at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) 
! at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) 
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) 
! at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) 
! at java.lang.Thread.run(Thread.java:745) 
192.168.1.224 - - [03/Mar/2017:02:14:37 +0000] "GET /smsnotification/sendsms HTTP/1.1" 500 110 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" 8177 

我敢在晚上敲我的頭撞在牆上做....如果有人能提供建議,請讓我知道。

回答

0

你缺少傑克遜依賴。將此添加到您的pom.xml中

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-core</artifactId> 
    <version>2.8.6</version> 
</dependency> 
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-guava --> 
<dependency> 
    <groupId>com.fasterxml.jackson.datatype</groupId> 
    <artifactId>jackson-datatype-guava</artifactId> 
    <version>2.0.3</version> 
</dependency> 
+0

謝謝。我會在今晚或明天回家後告訴你,情況如何。 – mike

+0

我添加了這些依賴關係,但應用程序在引導期間仍然崩潰。完全相同的錯誤。 :-( – mike

0

您的兩個依賴項正在提取不同版本的Jackson庫。這裏mvn dependency:tree -Dverbose是你的朋友,它會告訴你哪些依賴關係衝突:

... 
[INFO] +- io.dropwizard:dropwizard-core:jar:1.0.5:compile 
[INFO] | +- io.dropwizard:dropwizard-util:jar:1.0.5:compile 
[INFO] | | +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.7.8:compile - omitted for conflict with 2.4.3) 
... 
[INFO] \- com.twilio.sdk:twilio:jar:7.5.0:compile 
... 
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.3:compile 
[INFO] | +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0:compile - omitted for conflict with 2.4.3) 
... 

你可以在這裏看到dropwizard-coretwilio使用不同版本的不同傑克遜包。通常,當你看到這個時,Maven使用錯誤版本的機會很大,因爲它沒有足夠的信息。所以我們應該告訴它要使用哪個版本的Jackson(通常我們選擇最新版本)。有幾個方法可以做到這一點:

  1. 依賴管理:添加爭食依賴內該塊可以讓你選擇自己的整個項目的版本。添加下一個塊的<dependencies>外:

    <dependencyManagement> 
        <dependencies> 
         <dependency> 
          <groupId>com.fasterxml.jackson.core</groupId> 
          <artifactId>jackson-core</artifactId> 
          <version>2.7.8</version> 
         </dependency> 
         <dependency> 
          <groupId>com.fasterxml.jackson.core</groupId> 
          <artifactId>jackson-annotations</artifactId> 
          <version>2.7.8</version> 
         </dependency> 
         <dependency> 
          <groupId>com.fasterxml.jackson.core</groupId> 
          <artifactId>jackson-databind</artifactId> 
          <version>2.7.8</version> 
         </dependency> 
        </dependencies> 
    </dependencyManagement> 
    
  2. 對你的依賴排除包:這種方式給你一個更細粒度的控制。只需將<exclusions>塊添加到您要忽略這些軟件包版本的依賴項中即可。在這種情況下,將會是Twilio,因爲它有衝突庫的最舊版本。

    <dependency> 
        <groupId>com.twilio.sdk</groupId> 
        <artifactId>twilio</artifactId> 
        <version>7.5.0</version> 
        <exclusions> 
         <exclusion> 
          <groupId>com.fasterxml.jackson.core</groupId> 
          <artifactId>jackson-core</artifactId> 
         </exclusion> 
         <exclusion> 
          <groupId>com.fasterxml.jackson.core</groupId> 
          <artifactId>jackson-annotations</artifactId> 
         </exclusion> 
         <exclusion> 
          <groupId>com.fasterxml.jackson.core</groupId> 
          <artifactId>jackson-databind</artifactId> 
         </exclusion> 
        </exclusions> 
    </dependency> 
    

當你開始使用Maven這個問題會咬你了幾次,但dependencies插件是真正有用的在這裏。

我在twilio-java上創建了一個PR來升級這些依賴關係,但是如果您不想等待使用上述方法之一。

About Maven's dependency mechanism

+0

謝謝你,你的建議很有效,我可以編譯和運行服務器,但是,我收到了另外一個錯誤,我將用堆棧跟蹤和java類文件編輯我的帖子。 – mike

0

我找到了這個問題的真正答案。問題在於Ubuntu Mate for Raspberry Pi發行版中提供的JVM。他們提供OpenJDK。經過多次在線調試後,我開始將操作系統和JVM視爲我的問題的來源,並且當我將OpenJDK換成Oracle JDK時,一切都按照我的預期工作。