メニュー

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

JavaおよびSpringを使用したSMSおよびMMS通知

Today we'll build a Java and Spring sample application which demonstrates how to push SMS alerts on server exceptions. We'll show you how to automatically notify your server administrators when something goes wrong, and cover the plumbing that makes it happen.

See how EMC uses Twilio SMS to send IT alerts to 68,000 employees.

はじめましょう!

下のボタンをクリックして、チュートリアルの次の手順に移動してください。

Spring to It

List the Server Administrators - or Whomever Else - to Notify

Here we create a JSON list of administrators and whomever else should be notified if a server exception occurs.

The only essential piece of data we need is a phoneNumber for each person.

        
        
        
        
        src/main/resources/administrators.json

        JSONの管理者一覧

        src/main/resources/administrators.json

        Next, let's take a look at how to configure the Twilio REST client.

        Set that Client Up

        Twilio RESTクライアントの構成

        To send a message we'll need to initialize the TwilioRestClient as documented in the Twilio Java Helper Library. It requires reading a TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN from environment variables.

        The values for your Account SID and Authorization Token will come directly from the Twilio console:

        コンソール内のTwilioアカウントサマリーセクション

        Click the eyeball icon to expose your Auth Token in a form you can copy and paste.

              
              
              
              
              src/main/java/com/twilio/notifications/domain/twilio/Client.java

              Twilio クライアントを設定する

              src/main/java/com/twilio/notifications/domain/twilio/Client.java

              Next, we will see how to handle application exceptions and add in our new functionality.

              アプリケーションの例外を処理する

              Spring MVC provides several techniques to handle errors but in this case we are interested in handling all our application exceptions on our own.

              For catching everything, we can use Global Exception Handling.

                    
                    
                    
                    
                    src/main/java/com/twilio/notifications/controller/GlobalExceptionController.java

                    Catch all exceptions in Spring with a Global Exception Controller

                    src/main/java/com/twilio/notifications/controller/GlobalExceptionController.java

                    Next up, let's see how to create a custom message.

                    Creating a Custom Alert Message

                    Here we craft the perfect alert message to send out via text message.

                    You might also decide to include a picture with your exception handling message. Perhaps a screenshot of the application when the crash happened? Some infographics from somewhere?

                          
                          
                          
                          
                          src/main/java/com/twilio/notifications/controller/GlobalExceptionController.java

                          カスタム例外メッセージ

                          src/main/java/com/twilio/notifications/controller/GlobalExceptionController.java

                          Let's look at how to load the list of lucky administrators.

                          Load the Lucky Administrator List

                          JSONファイルから管理者の一覧を読み込む

                          Next we read the admins (and the other lucky folks) from our JSON file.

                          We use the Gson Java library to convert our JSON text file into Administrator objects from our application.

                                
                                
                                
                                
                                src/main/java/com/twilio/notifications/service/AdministratorService.java

                                Read the administrators from the JSON File

                                src/main/java/com/twilio/notifications/service/AdministratorService.java

                                Next up, let's look at how to send a text message.

                                SMSを送信

                                テキストメッセージの送信

                                There are three parameters needed to send an SMS using the Twilio REST API: From, To, and Body.

                                US and Canadian phone numbers can also send an image with the message. Other countries can as well, but the image will be included in the message body as a shortened URL.

                                      
                                      
                                      
                                      
                                      src/main/java/com/twilio/notifications/domain/twilio/TwilioMessageCreator.java

                                      TwilioMessageCreator wrapper class

                                      src/main/java/com/twilio/notifications/domain/twilio/TwilioMessageCreator.java

                                      これで以上です!

                                      We've just implemented an automated server notification system in Java and Spring that notifies all the right people when exceptions inevitably occur.

                                      Now let's look at some other common features that are easy to add with the Java SDK.

                                      Spring has Sprung; Grow Some New Features

                                      次はどこでしょうか?

                                      Twilio を使う Java 開発者であれば、他のチュートリアルも調べてみてください。

                                      自動アンケート

                                      Don't let that valuable marketing data slip away! Instantly collect structured data from your users with a survey conducted over a voice call or SMSes.

                                      アポイントメント リマインダー

                                      Prevent no-shows by building an awesome Twilio-powered appointment reminder feature into your application.

                                      これは役に立ちましたか?

                                      このチュートリアルをご覧いただき、ありがとうございます。 

                                      Tweet to us @twilio and let us know how it went, or what you're building next!

                                      Agustin Camino Paul Kamp Andrew Baker David Prothero Kat King Samuel Mendes
                                      Rate this page:

                                      ヘルプが必要ですか?

                                      誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。