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

如何在Spring Boot应用中使用HAPI FHIR结构?接口报415媒体类型错误

解决Spring Boot中HAPI FHIR接口415 Unsupported Media Type错误

这个415错误我之前也碰到过,本质是Spring默认的JSON转换器搞不定FHIR特有的JSON结构——FHIR的JSON有自己专属的规范,和普通POJO的JSON序列化逻辑不一样,所以Spring没法把请求里的JSON自动转成HAPI的Bundle对象。下面给你一步步解决的方案:

1. 先确认引入了正确的HAPI FHIR依赖

首先要确保你的项目里引入了对应FHIR版本的HAPI结构包和基础包,比如如果是DSTU3版本,Maven的pom.xml里加这些依赖:

<dependency>
    <groupId>ca.uhn.hapi.fhir</groupId>
    <artifactId>hapi-fhir-structures-dstu3</artifactId>
    <!-- 建议用最新稳定版,比如6.2.0,注意所有HAPI包版本要一致 -->
    <version>6.2.0</version>
</dependency>
<dependency>
    <groupId>ca.uhn.hapi.fhir</groupId>
    <artifactId>hapi-fhir-base</artifactId>
    <version>6.2.0</version>
</dependency>

如果用Gradle的话,对应的依赖配置是:

implementation 'ca.uhn.hapi.fhir:hapi-fhir-structures-dstu3:6.2.0'
implementation 'ca.uhn.hapi.fhir:hapi-fhir-base:6.2.0'

2. 配置HAPI专属的消息转换器

Spring需要知道怎么处理FHIR的JSON,所以要注册HAPI提供的HapiFhirJsonMessageConverter。创建一个WebMvc配置类:

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.server.messaging.HapiFhirJsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class FhirWebConfig implements WebMvcConfigurer {

    private final FhirContext fhirContext;

    // 根据你支持的FHIR版本初始化FhirContext,这里以DSTU3为例
    public FhirWebConfig() {
        this.fhirContext = FhirContext.forDstu3();
    }

    @Bean
    public HapiFhirJsonMessageConverter hapiFhirJsonMessageConverter() {
        return new HapiFhirJsonMessageConverter(fhirContext);
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 把HAPI的转换器加入Spring的转换器列表
        converters.add(hapiFhirJsonMessageConverter());
    }
}

如果你的应用需要支持多个FHIR版本(比如DSTU3、R4),可以考虑根据请求路径里的fhirVersion动态选择对应的FhirContext,不过先搞定单版本的情况再说。

3. 优化你的接口方法(更规范的写法)

建议把接口的返回类型改成Bundle而不是String,同时明确指定接口支持的媒体类型——FHIR标准的JSON媒体类型是application/fhir+json,不过有些客户端可能会发application/json,所以可以都加上:

import ca.uhn.fhir.model.dstu3.resource.Bundle;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/{fhirVersion}")
public class FhirTransactionController {

    @PostMapping(value = "/$transaction",
                consumes = {"application/json", "application/fhir+json"},
                produces = {"application/json", "application/fhir+json"})
    public Bundle executeTransaction(@RequestBody Bundle bundle,
                                     @PathVariable String fhirVersion) {
        // 这里写你的transaction处理逻辑,比如调用HAPI的服务处理Bundle
        // 示例:暂时返回原Bundle,实际要替换成业务逻辑
        return bundle;
    }
}

这样配置后,Spring会自动用HAPI的转换器处理请求的JSON解析和响应的JSON序列化,不用自己手动转字符串,更符合FHIR的规范。

4. 测试验证

发送请求时,优先用application/fhir+json作为Content-Type,不过配置完转换器后,application/json也能被正确解析了,不会再出现415错误。

简单来说,核心就是让Spring用上HAPI专门的JSON转换器,它能理解FHIR资源的特殊结构,解决默认转换器不兼容的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:21:35