1. 必要工具概述
为了有效实践FHIR,您需要准备以下工具:
| 工具 | 目的 | 需要嗎? |
|---|---|---|
| HAPI FHIR 伺服器 | FHIR 伺服器本地用於練習 | 是的 |
| 碼頭工人 | 运行 HAPI FHIR 服务器 | 是的 |
| 郵差/布魯諾 | 測試介面 | 是的 |
| VS程式碼 | 具有 FHIR 扩展的编辑器 | 是的 |
| Node.js | 運行 SUSHI(FHIR 速記編譯器) | 推薦 |
| 爪哇 17+ | HAPI FHIR 用戶端程式庫 | 選項 |
2.使用Docker安装HAPI FHIR服务器
哈皮FHIR 是最流行的开源 FHIR 服务器,用 Java 编写。让它运行的最快方法是使用 Docker。
步驟1:建立docker-compose.yml文件
version: "3.8"
services:
hapi-fhir:
image: hapiproject/hapi:latest
container_name: hapi-fhir-server
ports:
- "8080:8080"
environment:
- hapi.fhir.fhir_version=R5
- hapi.fhir.allow_multiple_delete=true
- hapi.fhir.allow_external_references=true
- hapi.fhir.reuse_cached_search_results_millis=-1
volumes:
- hapi-data:/data/hapi
restart: unless-stopped
volumes:
hapi-data:
第 2 步:啟動伺服器
# Tạo thư mục dự án
mkdir fhir-lab && cd fhir-lab
# Tạo docker-compose.yml (nội dung ở trên)
# Khởi chạy
docker compose up -d
# Kiểm tra log
docker compose logs -f hapi-fhir
等待大約30-60秒,伺服器將準備就緒 http://localhost:8080.
第三步:检查服务器
# Lấy CapabilityStatement
curl -s http://localhost:8080/fhir/metadata | jq '.fhirVersion'
# Kết quả: "5.0.0"
# Kiểm tra resource types được hỗ trợ
curl -s http://localhost:8080/fhir/metadata | jq '.rest[0].resource | length'
# Kết quả: 157 (hoặc gần đó)
HAPI FHIR 網頁使用者介面
訪問 http://localhost:8080 进入 HAPI FHIR Web 测试 UI — 一个 Web 界面,允许您执行所有 FHIR 操作而无需编写代码。
3. 公共FHIR測試伺服器
除了本地服务器之外,还可以使用公共服务器进行测试:
| 伺服器 | 網址 | 版本 | 註解 |
|---|---|---|---|
| HAPI FHIR R5 | https://hapi.fhir.org/baseR5 | R5 | Public, 免費,定期重置 |
| HAPI FHIR R4 | https://hapi.fhir.org/baseR4 | R4 | Public, 免費 |
| 火力伺服器 | https://server.fire.ly | R4 | Sandbox 免費 |
| 智慧健康資訊技術 | https://launch.smarthealthit.org | R4 | SMART 在 FHIR 沙箱上 |
注意: 不要将真实数据发布到公共服务器。仅使用测试/合成数据。
4. 为 FHIR 配置 Postman
第 1 步:建立集合
在 Postman 中使用变量创建集合“FHIR Lab”:
基本網址=http://localhost:8080/fhir
第 2 步:创建基本请求
1.CapabilityStatement(取得元資料)
GET {{baseUrl}}/metadata
Accept: application/fhir+json
2. 创建患者(POST)
POST {{baseUrl}}/Patient Content-Type: application/fhir+json Accept: application/fhir+json
{ "resourceType": "Patient", "identifier": [{ "system": "http://fhir.vn/sid/cccd", "value": "001085012345" }], "name": [{ "use": "official", "family": "Nguyễn", "given": ["Văn", "A"] }], "gender": "male", "birthDate": "1985-03-15", "address": [{ "use": "home", "line": ["123 Lê Lợi, Quận 1"], "city": "Thành phố Hồ Chí Minh", "country": "VN" }], "telecom": [{ "system": "phone", "value": "+84901234567", "use": "mobile" }] }
3. 讀取患者 (GET)
GET {{baseUrl}}/Patient/{{patientId}}
Accept: application/fhir+json
4. 搜尋患者 (GET)
GET {{baseUrl}}/Patient?family=Nguyen&gender=male
Accept: application/fhir+json
5. 更新病患 (PUT)
PUT {{baseUrl}}/Patient/{{patientId}}
Content-Type: application/fhir+json
Accept: application/fhir+json
5. FHIR 的 VS 代碼擴展
安裝有用的擴充功能:
FHIR 工具(建議)
FHIR 簡寫 (SUSHI/HL7) — FSH 的語法反白顯示
休息客戶端 (Huachao Mao) — 直接从 VS Code 发送 HTTP 请求
JSON 路徑查找器 — 導航大型 JSON
在 VS Code 中使用 REST 客户端
建立文件 fhir-test.http:
@baseUrl = http://localhost:8080/fhir
### Metadata
GET {{baseUrl}}/metadata
Accept: application/fhir+json
### Create Patient
POST {{baseUrl}}/Patient
Content-Type: application/fhir+json
{
"resourceType": "Patient",
"name": [{"family": "Trần", "given": ["Thị", "B"]}],
"gender": "female",
"birthDate": "1990-07-20"
}
### Search all patients
GET {{baseUrl}}/Patient?_count=10
Accept: application/fhir+json
单击每个要发送的请求上方的“发送请求”。
6. FHIR 簡寫(FSH)和 SUSHI
FHIR 簡寫 (FSH) 是一種簡潔地編寫設定檔、擴充、值集的規範語言。 壽司 是一個將FSH編譯成FHIR資源的編譯器。
安裝壽司
# Cài Node.js (nếu chưa có)
# macOS
brew install node
# Cài SUSHI globally
npm install -g fsh-sushi
# Kiểm tra
sushi --version
簡單的 FSH 範例
Profile: VNPatient
Parent: Patient
Id: vn-patient
Title: "Vietnam Core Patient Profile"
Description: "Profile cho bệnh nhân Việt Nam"
* identifier 1..* MS
* identifier ^slicing.discriminator.type = #pattern
* identifier ^slicing.discriminator.path = "system"
* identifier ^slicing.rules = #open
* identifier contains cccd 0..1 MS
* identifier[cccd].system = "http://fhir.vn/sid/cccd"
* identifier[cccd].value 1..1
* name 1..* MS
* gender 1..1 MS
* birthDate 1..1 MS
FSH 將在第 13 課(摘要)和第 20 課(實施指南)中廣泛使用。
7.實作:首先CRUD操作
現在,我們來練習 HAPI FHIR Server 的基本操作。
7.1.創建患者(創建)
curl -X POST http://localhost:8080/fhir/Patient \
-H "Content-Type: application/fhir+json" \
-d '{
"resourceType": "Patient",
"identifier": [{
"system": "http://fhir.vn/sid/cccd",
"value": "001085012345"
}],
"name": [{
"use": "official",
"family": "Nguyễn",
"given": ["Văn", "A"]
}],
"gender": "male",
"birthDate": "1985-03-15",
"address": [{
"line": ["123 Lê Lợi"],
"city": "Hồ Chí Minh",
"country": "VN"
}]
}'
伺服器回 201 已建立 帶標題 位置:病人/{id}/_history/1。
7.2.閱讀患者(閱讀)
# Thay {id} bằng id thực tế được trả về
curl -s http://localhost:8080/fhir/Patient/{id} | jq '.name[0]'
7.3.搜尋患者 (SEARCH)
# Tìm theo họ
curl -s "http://localhost:8080/fhir/Patient?family=Nguyen" | jq '.total'
# Tìm theo giới tính
curl -s "http://localhost:8080/fhir/Patient?gender=male" | jq '.entry | length'
# Tìm theo identifier (số CCCD)
curl -s "http://localhost:8080/fhir/Patient?identifier=http://fhir.vn/sid/cccd|001085012345" | jq '.entry[0].resource.name'
7.4.更新病人(更新)
curl -X PUT http://localhost:8080/fhir/Patient/{id} \
-H "Content-Type: application/fhir+json" \
-d '{
"resourceType": "Patient",
"id": "{id}",
"identifier": [{
"system": "http://fhir.vn/sid/cccd",
"value": "001085012345"
}],
"name": [{
"use": "official",
"family": "Nguyễn",
"given": ["Văn", "A"]
}],
"gender": "male",
"birthDate": "1985-03-15",
"telecom": [{
"system": "phone",
"value": "+84901234567",
"use": "mobile"
}],
"address": [{
"line": ["456 Nguyễn Huệ"],
"city": "Hồ Chí Minh",
"country": "VN"
}]
}'
7.5。刪除患者(DELETE)
curl -X DELETE http://localhost:8080/fhir/Patient/{id}
# Trả về 200 OK hoặc 204 No Content
7.6。查看歷史記錄(HISTORY)
# Lịch sử một resource cụ thể
curl -s http://localhost:8080/fhir/Patient/{id}/_history | jq '.entry | length'
# Lịch sử toàn bộ Patient type
curl -s "http://localhost:8080/fhir/Patient/_history?_count=5" | jq '.entry[].request.method'
8.建立批次測試數據
要獲得豐富的實踐數據,請使用 辛西亞 — 創建綜合病患資料的工具:
# Cài Synthea (cần Java 11+)
git clone https://github.com/synthetichealth/synthea.git
cd synthea
# Tạo 10 bệnh nhân
./run_synthea -p 10 --exporter.fhir.export=true
# Output sẽ nằm trong output/fhir/
ls output/fhir/
輸出檔是FHIR Bundle(交易),可以直接POST到伺服器:
# Upload một bundle lên server
curl -X POST http://localhost:8080/fhir \
-H "Content-Type: application/fhir+json" \
-d @output/fhir/hospitalInformation1234.json
9. 推薦的項目結構
fhir-lab/
├── docker-compose.yml # HAPI FHIR Server
├── http/ # REST Client files
│ ├── patient.http
│ ├── observation.http
│ └── search.http
├── fsh/ # FHIR Shorthand files
│ ├── input/
│ │ └── fsh/
│ │ ├── profiles/
│ │ ├── extensions/
│ │ └── valuesets/
│ └── sushi-config.yaml
├── sample-data/ # Dữ liệu mẫu
│ ├── patients/
│ ├── observations/
│ └── bundles/
└── scripts/ # Scripts tiện ích
├── load-data.sh
└── cleanup.sh
10. 總結
在這篇文章中,我們有:
安裝 HAPI FHIR 伺服器 使用 Docker(連接埠 8080)
知道 FHIR 測試伺服器 公共 (hapi.fhir.org)
配置 郵差 和 VS 代碼 REST 用戶端 對於FHIR
安裝 壽司 (FHIR 速記編譯器)
練習 增刪改查操作:建立、讀取、搜尋、更新、刪除、歷史記錄
使用 辛西亞 建立綜合測試數據
下一篇文章我們將開始深入探討 FHIR 資源 — 從病人、從業人員、組織和行政資源開始。