springbootはテンセントクラウドメッセージングサービスを統合しました:
(1)依存性を導入
<依存>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>春・ブート・スターター・ウェブ</たartifactId>
</依存関係>
<依存>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>ばねブートスタータ試験</たartifactId>
<スコープ>テスト</スコープ>
<除外>
<除外>
<groupIdを> com.vaadin.external.google </ groupIdを>
<たartifactId>アンドロイド-JSON </たartifactId>
</除外>
</除外>
</依存関係>
<依存>
<groupIdを> com.github.qcloudsms </ groupIdを>
<たartifactId> qcloudsms </たartifactId>
<バージョン> 1.0.6 </バージョン>
</依存関係>
(2)グローバルコンフィギュレーションファイルにサービスコードおよび他の情報を
#のSMSアプリSDKのAppID
tx.sms.appid = *************
#のSMSアプリSDKのAppKey
tx.sms.appkey = ***********
使用#シグニチャパラメータは、 ``署名コンテンツであります
tx.sms.smsSign = 公共の数を考えてイチゴノキ
#SMSテンプレートIDは、テキストメッセージのアプリケーションに適用する必要があります
tx.sms.templateId = *********
(3)抽出ツール
@Component
パブリック クラスSmsUtil {
@value( "$ {tx.sms.appid}" )、
プライベート整数のappid。
@value( "$ {tx.sms.appkey}" )
プライベート文字列APPKEY。
@value( "$ {tx.sms.smsSign}")
プライベート文字列smsSign。
@value( "$ {tx.sms.templateId}" )、
プライベート整数れるtemplateId。
公共 ボイド txSmsSend(文字列にphoneNumber、のArrayList <ストリング> のparams){
// 7.パッケージメッセージングアプリケーションコード
SmsSingleSenderのssenderは= 新しい新しいSmsSingleSender(AppIDを、のAppKey);
// エリア、電話、テンプレートID、コードは、シグネチャ
試して{
結果SmsSingleSenderResult = ssender.sendWithParam(
"86"にphoneNumber、れるtemplateId、paramsは、smsSign、 ""、 "" );
// 戻り値で出力
するSystem.out.println(結果)。
} キャッチ(HTTPException | JSONException | のIOException電子){
// TODO自動生成キャッチブロック
e.printStackTrace();
}
}
}
(4)試験
@RunWith(SpringJUnit4ClassRunner。クラス)
@SpringBootTest(クラス = PhoneApp。クラス)
パブリック クラスのテスト{
@Autowired
プライベートSmsUtilのSMS;
@ org.junit.Test
公共 ボイド試験(){
文字列にphoneNumber = "17609584519" 。
ArrayListの <文字列>リスト= 新しい ArrayListを<> ();
list.add("赵丽颖");
list.add("lyf");
list.add("2019-08-13");
sms.txSmsSend(phoneNumber, list);
}
}
结果截图:
可以看到:短信内容中有一段信息是乱码的
原因分析:
是由于在application.properties配置文件中读取 tx.sms.smsSign信息时,由于属性值是汉字,编码格式不一致,导致的
解决方案:
方式一:将 tx.sms.smsSign信息在程序中进行设置,如下:
private String smsSign = "草莓树下的思考公众号";
方式二:在application.properties中定义,但是在传参时要进行编码转换
application.properties配置文件
#签名参数使用的是`签名内容`
tx.sms.smsSign=草莓树下的思考公众号
工具类:
@Value("${tx.sms.smsSign}")
private String smsSign;
smsSign = new String(smsSign.getBytes("ISO8859-1"), "UTF-8");
最终结果截图:
谢谢支持!!!