2

我正在尝试使用 twillio 发送短信,代码如下:

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import org.apache.http.NameValuePair;
import org.apache.http.auth.Credentials;
import org.apache.http.message.BasicNameValuePair;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class SendSms extends HttpServlet {
    private Logger logger = Logger.getLogger(SendSms.class.getName());
    private TwilioRestClient client;
    private Credentials credentials;

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException,
        ServletException {
    final String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
    final String twilioAuthToken = System.getenv("TWILIO_AUTH_TOKEN");
    final String twilioNumber = System.getenv("TWILIO_NUMBER");
    final String toNumber = (String) twilioNumber;
    String message = "prova";
    client = new TwilioRestClient(twilioAccountSid, twilioAuthToken);
    sendMessage(toNumber, message);
}

public void sendMessage(String to, String message) {
    List<NameValuePair> params = getParams(to, message);

    try {
        this.client.getAccount().getMessageFactory().create(params);
    } catch (TwilioRestException exception) {
        exception.printStackTrace();
    }
}

private List<NameValuePair> getParams(String to, String message) {
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("Body", message));
    params.add(new BasicNameValuePair("To", to));
    params.add(new BasicNameValuePair("From", to));

    return params;
}

}

只是一个测试,所以我不关心性能、代码清理等。调用 servlet 我收到以下错误:

WARNING: Error for /SendSms
java.lang.NoClassDefFoundError: com/twilio/sdk/TwilioRestException

我正在使用 maven,导入相关的 twillio api,这里是 pom 依赖:

<dependencies>
    <dependency>
        <groupId>com.twilio.sdk</groupId>
        <artifactId>twilio-java-sdk</artifactId>
        <version>(6.0,6.9)</version>
    </dependency>

有人有什么建议吗?先感谢您。

4

2 回答 2

0

也许缺少<scope>compile是问题:

<dependency>
  <groupId>com.twilio.sdk</groupId>
  <artifactId>twilio-java-sdk</artifactId>
  <version>(6.0,6.9)</version>
  <scope>compile</scope>
</dependency>

有关开发环境的完整详细说明:

https://www.twilio.com/docs/quickstart/java/devenvironment#pomxml

于 2016-09-14T23:36:50.053 回答
0

我有一个像这样分层的疲惫的应用程序

WebServices 服务 DAO 通用

我在服务模块中调用 twilio,所以我将 maven 依赖项放在该 maven 模块的 pom 文件中。出于某种原因,我不断收到与您相同的错误。将依赖项添加到 WebServices 模块解决了该问题

于 2017-09-22T04:15:09.187 回答