1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

QtによるRESTfull API構築 ~Server編~

Last updated at Posted at 2025-10-10

はじめに

Qtの環境構築からRestAPI Serverのサンプルをビルドして実行するまでの、手順について記載します。
Qt Group DocumentaionのRESTful API Server
サンプルプロジェクトがオンライン上にアップされていないので、プロジェクトから作成します。
幸い、ソース一式はアップされているので、colorpaletteserverプロジェクトを作成してCMakeList.txtの変更とデータとなるJSONファイルの作成をすれば動作すると思い、トライしてみます。

実施環境

次の環境下で実施しました。IPアドレスは各環境に応じたものを使用します。

OS IPアドレス
Windows11 192.168.3.77
Ubuntu24.04.3(Hyper-V) 192.168.3.83

Qt(6.9.3)インストール

1. Linux用オンラインインストーラーをダウンロード

Qtオンラインインストーラー置き場からqt-online-installer-linux-x64-online.runをダウンロードすると、以下のような<version>が付与されたファイルになります。
qt-online-installer-linux-x64-<version>.run

2. インストール

任意のディレクトリにインストーラーを置き、次のコマンドを実行します。

apt install -y libxcb-cursor0
chmod +x qt-online-installer-linux-x64-<version>.run
./qt-online-installer-linux-x64-<version>.run

コマンドはrootユーザーで実行していますが、rootユーザー以外の場合は必要に応じてsudoを付与して実行して下さい。

インストールには事前にQtのユーザー登録が必要になります。

以降のインストール設定は、全てデフォルトの状態で問題ないですが、必要に応じてインストール先ディレクトリや追加するパッケージやコンポーネントを設定して下さい。

colorpalletserver構築

1. 初期設定

ビルドするにはコンパイラであるgccまたはclangとninjaが必要になりますので、ここではbuild-essentialをインストールします。ninjaはninja-buildをインストールするか、ダウンロードされた/opt/Qt/Tools/Ninja/ninjaのリンクを/usr/bin/に作成します。

apt install -y build-essential
ln -s /opt/Qt/Tools/Ninja/ninja /usr/bin/ninja

もしくは

apt install -y build-essential ninja-build

加えて、colorpalletserverにはQtのGuiコンポーネントが必要になるのですが、これを動作させる為にlibgl1-mesa-devをインストールします。

apt install -y libgl1-mesa-dev

2. QtCreatorの起動

QtCreatorを起動します。デフォルトの保存場所は以下になります。

/opt/Qt/Tools/QtCreator/bin/qtcreator

起動しやすいようにHomeディレクトリにリンクを貼っておくと使いやすいかと思います。

ln -s /opt/Qt/Tools/QtCreator/bin/qtcreator <path to home>/qtcreator

3. 各JSONデータ作成

公式ドキュメントにはusers.json, colors.json, sessions.jsonがアップロードされておらず、一部の情報だけしか掲載されていなかったので、types.hからJSONの定義を読み取り、テストデータはChatGPTを使用して生成しました。こういう時、AIは便利ですね。

users.json
[
  {
    "email": "alex.smith@qt.io",
    "first_name": "Alex",
    "last_name": "Smith",
    "avatar": "/img/faces/1-image.jpg",
    "createAt": "2025-10-03T01:03:04.042Z",
    "updatedAt": "2025-10-03T01:03:04.042Z"
  },
  {
    "email": "lisa.jones@qt.io",
    "first_name": "Lisa",
    "last_name": "Jones",
    "avatar": "/img/faces/2-image.jpg",
    "createAt": "2025-10-03T01:04:01.000Z",
    "updatedAt": "2025-10-03T01:04:01.000Z"
  },
  {
    "email": "michael.brown@qt.io",
    "first_name": "Michael",
    "last_name": "Brown",
    "avatar": "/img/faces/3-image.jpg",
    "createAt": "2025-10-03T01:05:15.100Z",
    "updatedAt": "2025-10-03T01:05:15.100Z"
  },
  {
    "email": "emma.wilson@qt.io",
    "first_name": "Emma",
    "last_name": "Wilson",
    "avatar": "/img/faces/4-image.jpg",
    "createAt": "2025-10-03T01:06:20.120Z",
    "updatedAt": "2025-10-03T01:06:20.120Z"
  },
  {
    "email": "daniel.taylor@qt.io",
    "first_name": "Daniel",
    "last_name": "Taylor",
    "avatar": "/img/faces/5-image.jpg",
    "createAt": "2025-10-03T01:07:33.200Z",
    "updatedAt": "2025-10-03T01:07:33.200Z"
  },
  {
    "email": "sophia.moore@qt.io",
    "first_name": "Sophia",
    "last_name": "Moore",
    "avatar": "/img/faces/6-image.jpg",
    "createAt": "2025-10-03T01:08:40.250Z",
    "updatedAt": "2025-10-03T01:08:40.250Z"
  },
  {
    "email": "jack.miller@qt.io",
    "first_name": "Jack",
    "last_name": "Miller",
    "avatar": "/img/faces/7-image.jpg",
    "createAt": "2025-10-03T01:09:59.310Z",
    "updatedAt": "2025-10-03T01:09:59.310Z"
  },
  {
    "email": "olivia.davis@qt.io",
    "first_name": "Olivia",
    "last_name": "Davis",
    "avatar": "/img/faces/8-image.jpg",
    "createAt": "2025-10-03T01:10:12.360Z",
    "updatedAt": "2025-10-03T01:10:12.360Z"
  },
  {
    "email": "ethan.garcia@qt.io",
    "first_name": "Ethan",
    "last_name": "Garcia",
    "avatar": "/img/faces/9-image.jpg",
    "createAt": "2025-10-03T01:11:45.390Z",
    "updatedAt": "2025-10-03T01:11:45.390Z"
  },
  {
    "email": "mia.martinez@qt.io",
    "first_name": "Mia",
    "last_name": "Martinez",
    "avatar": "/img/faces/10-image.jpg",
    "createAt": "2025-10-03T01:12:22.420Z",
    "updatedAt": "2025-10-03T01:12:22.420Z"
  },
  {
    "email": "william.rodriguez@qt.io",
    "first_name": "William",
    "last_name": "Rodriguez",
    "avatar": "/img/faces/11-image.jpg",
    "createAt": "2025-10-03T01:13:30.500Z",
    "updatedAt": "2025-10-03T01:13:30.500Z"
  },
  {
    "email": "ava.lee@qt.io",
    "first_name": "Ava",
    "last_name": "Lee",
    "avatar": "/img/faces/12-image.jpg",
    "createAt": "2025-10-03T01:14:44.600Z",
    "updatedAt": "2025-10-03T01:14:44.600Z"
  }
]
colors.json
[
  {
    "name": "Alice Blue",
    "color": "aliceblue",
    "pantone_value": "656-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Antique White",
    "color": "antiquewhite",
    "pantone_value": "7527-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Aqua",
    "color": "aqua",
    "pantone_value": "298-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Aquamarine",
    "color": "aquamarine",
    "pantone_value": "324-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Azure",
    "color": "azure",
    "pantone_value": "290-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Beige",
    "color": "beige",
    "pantone_value": "468-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Bisque",
    "color": "bisque",
    "pantone_value": "162-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Black",
    "color": "black",
    "pantone_value": "Black-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Blanched Almond",
    "color": "blanchedalmond",
    "pantone_value": "4685-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Blue",
    "color": "blue",
    "pantone_value": "2728-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Blue Violet",
    "color": "blueviolet",
    "pantone_value": "2096-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Brown",
    "color": "brown",
    "pantone_value": "4975-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Burly Wood",
    "color": "burlywood",
    "pantone_value": "467-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Cadet Blue",
    "color": "cadetblue",
    "pantone_value": "5483-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Chartreuse",
    "color": "chartreuse",
    "pantone_value": "389-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Chocolate",
    "color": "chocolate",
    "pantone_value": "7580-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Coral",
    "color": "coral",
    "pantone_value": "2345-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Cornflower Blue",
    "color": "cornflowerblue",
    "pantone_value": "2380-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Cornsilk",
    "color": "cornsilk",
    "pantone_value": "7506-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  },
  {
    "name": "Crimson",
    "color": "crimson",
    "pantone_value": "200-C",
    "createdAt": "2025-10-03T01:25:00.000Z",
    "updatedAt": "2025-10-03T01:25:00.000Z"
  }
]
sessions.json
[
  {
    "email": "alex.smith@qt.io",
    "password": "3AN8N!k1#Z7J"
  },
  {
    "email": "lisa.jones@qt.io",
    "password": "9j4eBUO%xq*3"
  },
  {
    "email": "michael.brown@qt.io",
    "password": "3SH1vcfHL1?D"
  },
  {
    "email": "emma.wilson@qt.io",
    "password": "Wk&@Ysao@9Vc"
  },
  {
    "email": "daniel.taylor@qt.io",
    "password": "r%M@Pc6fmRar"
  },
  {
    "email": "sophia.moore@qt.io",
    "password": "Q8jE2qR7Zy#?"
  },
  {
    "email": "jack.miller@qt.io",
    "password": "I44tg5FxD%qN"
  },
  {
    "email": "olivia.davis@qt.io",
    "password": "!uh?3qcjM5Q#"
  },
  {
    "email": "ethan.garcia@qt.io",
    "password": "AJyIqUHBv10?"
  },
  {
    "email": "mia.martinez@qt.io",
    "password": "AmLz6@?6$SiI"
  },
  {
    "email": "william.rodriguez@qt.io",
    "password": "XSE#l7Q04I6i"
  },
  {
    "email": "ava.lee@qt.io",
    "password": "&RrZMt*975CO"
  }
]

4. colorpalletserverプロジェクト作成

qtcreator起動 > 「Create Project」 > 「Qt Console Application」を選択して、任意のディレクトリにcolorpalletserverプロジェクトを作成し、後はすべてデフォルトで設定します。
次のようなディレクトリ構成で必要なファイルを置きます。

各ファイルの変更

CMakeList.txtは公式ドキュメントのCMakeList.txtを参考にします。

CMakeList.txt
cmake_minimum_required(VERSION 3.16)

project(colorpaletteserver LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS HttpServer Gui Concurrent)

add_executable(colorpaletteserver
  main.cpp
  apibehavior.h
  types.h
  utils.h
)

qt_add_resources(colorpaletteserver "assets"
    PREFIX "/"
    FILES
    assets/colors.json
    assets/users.json
    assets/sessions.json
    assets/img/1-image.jpg
    assets/img/2-image.jpg
    assets/img/3-image.jpg
    assets/img/4-image.jpg
    assets/img/5-image.jpg
    assets/img/6-image.jpg
    assets/img/7-image.jpg
    assets/img/8-image.jpg
    assets/img/9-image.jpg
    assets/img/10-image.jpg
    assets/img/11-image.jpg
    assets/img/12-image.jpg
)

target_link_libraries(colorpaletteserver PRIVATE
    Qt::HttpServer
    Qt::Gui
    Qt::Concurrent
)

include(GNUInstallDirs)
install(TARGETS colorpaletteserver
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

CMakeList.txtを保存すると、必要なコンポーネントが足りないとの事でQt Maintenance Toolが起動しますが、「Update」を押下してコンポーネントを追加します。

IPアドレスを実行環境に合わせます。

main.cpp
- #define HOST 127.0.0.1
+ #define HOST 192.168.3.83

5. ビルド・実行

QtCreator上でプロジェクトをビルドしcolorpaletteserverを起動します。

./colorpaletteserver
"Running on http://192.168.3.83:49425/ (Press CTRL+C to quit)"

動作確認

WindowsからAPIを実行してみます。今回はChromeの拡張機能のTrend API Testerを使用します。

METHOD > GET
SCHEMA > http://192.168.3.83:49425/api/users/1

で次のようなid=1のuserの情報が取得できます。

avatarのURIをGETすると、次のように画像データも取得できます。

おわりに

以上、Ubuntu(24.04.3)にQt(6.9.3)をインストールして、colorpalletserverを構築し、APIの動作確認をする流れを記載しました。
続きとして、サンプルのcolorpalletclientを参考にしてWindowsのGUIアプリケーションからAPIを実行して、GUI上に結果を表示するアプリケーションを作成してみます。

参考資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?