我正在尝试使用 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>
有人有什么建议吗?先感谢您。