You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

传入NULL枚举值时FHIR AllergyIntolerance的severity为何显示为"?"而非空串?

问题

我们基于HAPI FHIR R4(版本6.6.2)开发了一个将HL7 ADT消息转换为FHIR Bundle的应用。在处理AllergyIntolerance资源的reaction组件时,severity属性接受AllergyIntoleranceSeverity枚举值。当我们将该属性设置为AllergyIntoleranceSeverity.NULL后,把FHIR Bundle编码为JSON字符串,发现severity的值变成了"?",但我们期望它是空字符串"",请问这是什么原因?

示例代码

public static void main(String[] args) {
        
        // 创建Fhir上下文
        FhirContext r4Context = FhirContext.forR4();
        

        // 创建FHIR Bundle
        Bundle bundle = new Bundle();
        bundle.setId("bundle-uuid");
        

        // 创建AllergyIntolerance资源
        AllergyIntolerance allergyIntolerance = new AllergyIntolerance();
        allergyIntolerance.setId("alergy-uuid");

        // 创建AllergyIntolerance的reaction组件
        AllergyIntolerance.AllergyIntoleranceReactionComponent reaction= new AllergyIntolerance.AllergyIntoleranceReactionComponent();

        // AL1-4字段值为"U"
        // 根据映射规则,当AL1-4 == "U"时,severity设为NULL
        reaction.setSeverity(AllergyIntolerance.AllergyIntoleranceSeverity.NULL);

        // 将reaction添加到AllergyIntolerance资源
        allergyIntolerance.addReaction(reaction);


        // 将AllergyIntolerance资源添加到Bundle
        bundle.addEntry().setResource(allergyIntolerance);

        // 将Bundle转换为JSON字符串
        String fhirJson = r4Context.newJsonParser().encodeResourceToString(bundle);

        System.out.println(fhirJson);

    }

依赖配置

<dependency>
            <groupId>ca.uhn.hapi.fhir</groupId>
            <artifactId>hapi-fhir-structures-r4</artifactId>
            <version>6.6.2</version>
        </dependency>

输出结果

2023-08-03 12:40:54.943 [main] INFO  ca.uhn.fhir.util.VersionUtil:? -HAPI FHIR version 6.6.2 - Rev 8d55781507
2023-08-03 12:40:54.986 [main] INFO  ca.uhn.fhir.context.FhirContext:? -Creating new FHIR context for FHIR version [R4]
{
"resourceType": "Bundle",
"id": "bundle-uuid",
"entry": [
    {
        "resource": {
            "resourceType": "AllergyIntolerance",
            "id": "alergy-uuid",
            "reaction": [
                {
                    "severity": "?"
                }
            ]
        }
    }
]
}

原因及解决方案

原因

AllergyIntoleranceSeverity.NULL是HAPI FHIR中专门用来表示值未知/缺失的空值类型(null flavor),对应的FHIR规范编码逻辑就是将其序列化为"?"。这不是Bug,而是符合FHIR R4规范的设计:对于绑定了值集的枚举类型,当需要表示"未知"状态时,就用这个符号标记,空字符串并不是该字段的合法取值。

解决方案

根据业务需求,可选择以下处理方式:

  • 不输出severity字段:如果不需要传递该字段的任何信息,直接不调用reaction.setSeverity()方法,序列化后的JSON中就不会出现severity字段。
  • 接受"?"的输出:如果业务中"U"对应的就是"未知"状态,那么"?"是符合FHIR规范的正确输出,无需修改。
  • 自定义序列化(不推荐):如果业务强制要求空字符串,这违反FHIR规范,需自定义JSON序列化逻辑,但会导致生成的FHIR资源不符合标准,不建议这么做。

内容的提问来源于stack exchange,提问作者Jaiprasad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 17:23:25