https://start.spring.io/ をブラウザ開き、以下の部分を変更する。
- Project を
Maven
にする - Spring Boot を
2.7.6
にする - Artifact を
linebot
にする - Packaging を
jar
にする - Add Dependencies... をクリックし、表示されたウィンドウに
Web
と入力し、Spring Web
を選ぶ - Add Dependencies... をクリックし、表示されたウィンドウに
Thyme
と入力し、Thymeleaf
を選ぶ - Add Dependencies... をクリックし、表示されたウィンドウに
jdbc
と入力し、JDBC API
を選ぶ - Add Dependencies... をクリックし、表示されたウィンドウに
h2
と入力し、H2 Database
を選ぶ - 指示された部分以外はそのままでよい。
Generate ボタンを押して、zipファイルをダウンロードする。
ダウンロードした linebot.zip ファイルは展開しておく。
利用しているIDEにあわせて、それぞれ読み込ませてください。
- IntelliJ IDEAを使っている場合
- 開く(Open) で展開したフォルダの中の pom.xml を選択
- プロジェクトとして開く(Open as Project)
- プロジェクトを信頼するか聞かれた場合はプロジェクトを信頼(Trust Project)
- Eclipseを使っている場合
- ファイル > インポート > 既存Mavenプロジェクト で展開したフォルダを選択
- 読み込まれたら、プロジェクトを右クリックして Maven > プロジェクトの更新
必要なファイルが読み込まれるまで、時間がかかる可能性があります。
画面右下のインジケーターが終了するまで、静かに待ちましょう。
ソース・パッケージ(src/main/java)のcom.example.linebotパッケージの中にPushクラスを作る。
package com.example.linebot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class Push {
// テスト
@GetMapping("test")
public String hello(HttpServletRequest request) {
return "Get from " + request.getRequestURL();
}
}
LinebotApplication を右クリックで起動する。
ブラウザから、http://localhost:8080/test にアクセスする。以下の様に表示されれば成功。
Get from http://localhost:8080/test
ブラウザから、ngrokに表示されていた https://xxxxxxxx.jp.ngrok.io
を使って、 https://xxx.ngrok.io/test
にもアクセスする(xxxは各自のもの)。以下の様に表示されればOK。
Get from https://xxxxxxxx.jp.ngrok.io/test