基于HAPI JPA Server Starter创建自定义FHIR资源后无法发起请求
自定义FHIR资源无法请求的问题修复
我基于HAPI JPA Server Starter代码创建了自定义FHIR资源类型,但无法对该资源发起请求,操作步骤如下:
原操作步骤
1. 创建自定义资源类MyCustomResource
package ca.uhn.fhir.jpa.starter; import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.ResourceType; import org.hl7.fhir.r4.model.StringType; import ca.uhn.fhir.model.api.annotation.ResourceDef; @ResourceDef(name = "MyCustomResource", profile = "http://hl7.org/fhir/profiles/mycustom-resource") public class MyCustomResource extends Resource { public static final String SP_DIAGNOSIS = null; public static final String SP_CONFIDENCE_SCORE = null; private StringType diagnosis; private Integer confidenceScore; public StringType getDiagnosis() { return diagnosis; } public void setDiagnosis(StringType diagnosis) { this.diagnosis = diagnosis; } public Integer getConfidenceScore() { return confidenceScore; } public void setConfidenceScore(Integer confidenceScore) { this.confidenceScore = confidenceScore; } @Override public MyCustomResource copy() { MyCustomResource retVal = new MyCustomResource(); super.copyValues(retVal); retVal.diagnosis = diagnosis; retVal.confidenceScore = confidenceScore; return retVal; } @Override public ResourceType getResourceType() { return ResourceType.fromCode("MyCustomResource"); } }
2. 创建资源提供者类MyCustomResourceProvider
package ca.uhn.fhir.jpa.starter; import ca.uhn.fhir.jpa.provider.BaseJpaResourceProvider; public class MyCustomResourceProvider extends BaseJpaResourceProvider<MyCustomResource> { public MyCustomResourceProvider() { super(); } }
3. 在FhirServerConfigR4中注册资源和提供者
@Bean public FhirContext fhirContext(){ FhirContext ctx = new FhirContext(); //register custom resource ctx.registerCustomType(MyCustomResource.class); // register provider MyCustomResourceProvider provider = new MyCustomResourceProvider(); RestfulServer server = new RestfulServer(ctx); server.registerProvider(provider); return ctx; }
问题分析
原实现存在以下关键错误:
- ResourceType枚举不支持自定义值:
ResourceType.fromCode("MyCustomResource")会抛出异常,因为ResourceType是FHIR标准枚举,没有自定义资源的枚举项。 - 搜索参数定义无效:
SP_DIAGNOSIS和SP_CONFIDENCE_SCORE设为null,JPA提供者无法识别这些搜索参数。 - 资源注册方式错误:手动创建
RestfulServer实例不会被Spring容器管理,无法生效;自定义资源需要正确的FHIR注解才能被序列化/反序列化。 - 提供者构造参数缺失:
BaseJpaResourceProvider需要注入IResourceDao实例才能正常工作,默认构造函数无法提供必要的DAO支持。
修正后的实现
1. 修正自定义资源类
添加FHIR属性注解,修正资源类型返回逻辑:
package ca.uhn.fhir.jpa.starter; import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.StringType; import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.api.annotation.Description; @ResourceDef(name = "MyCustomResource", profile = "http://hl7.org/fhir/profiles/mycustom-resource") public class MyCustomResource extends Resource { public static final String SP_DIAGNOSIS = "diagnosis"; public static final String SP_CONFIDENCE_SCORE = "confidence-score"; @Child(name = "diagnosis", order = 0, min = 0, max = 1) @Description(shortDefinition = "诊断信息") private StringType diagnosis; @Child(name = "confidenceScore", order = 1, min = 0, max = 1) @Description(shortDefinition = "置信度分数") private Integer confidenceScore; public StringType getDiagnosis() { return diagnosis; } public void setDiagnosis(StringType diagnosis) { this.diagnosis = diagnosis; } public Integer getConfidenceScore() { return confidenceScore; } public void setConfidenceScore(Integer confidenceScore) { this.confidenceScore = confidenceScore; } @Override public MyCustomResource copy() { MyCustomResource retVal = new MyCustomResource(); super.copyValues(retVal); if (diagnosis != null) { retVal.diagnosis = diagnosis.copy(); } retVal.confidenceScore = confidenceScore; return retVal; } @Override public String getResourceType() { // 自定义资源直接返回资源名称,无需使用标准ResourceType枚举 return "MyCustomResource"; } }
2. 修正资源提供者类
注入IResourceDao实例:
package ca.uhn.fhir.jpa.starter; import ca.uhn.fhir.jpa.provider.BaseJpaResourceProvider; import ca.uhn.fhir.jpa.dao.IResourceDao; import org.springframework.beans.factory.annotation.Autowired; public class MyCustomResourceProvider extends BaseJpaResourceProvider<MyCustomResource> { @Autowired public MyCustomResourceProvider(IResourceDao<MyCustomResource> theDao) { super(theDao); } }
3. 修正配置类中的注册逻辑
通过Spring Bean方式注册,无需手动创建RestfulServer:
@Configuration public class FhirServerConfigR4 { // 注册自定义资源提供者 @Bean public MyCustomResourceProvider myCustomResourceProvider(IResourceDao<MyCustomResource> theDao) { return new MyCustomResourceProvider(theDao); } // 注册自定义资源类型到FhirContext @Bean public FhirContext fhirContext() { FhirContext ctx = FhirContext.forR4(); ctx.registerCustomType(MyCustomResource.class); return ctx; } }
4. 添加配置文件支持
在application.yml中启用自定义资源支持:
hapi: fhir: jpa: enable-custom-resources: true
内容的提问来源于stack exchange,提问作者Mayank Dixit
相关产品推荐
相关产品推荐

