java解析HL7协议报文工具 HAPI(SpringBoot版本)

该博客介绍了如何使用ca.uhn.hapi库解析和生成HL7协议报文,以及在遇到工具中未定义的报文结构时,如何自定义类来解析和构建报文。示例代码展示了如何创建自定义的Message、Composite类,并提供了工具类以方便操作HL7报文字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java解析HL7协议报文工具 因为项目需要解析HL7协议报文,网上找到的工具都是解析成带位置信息的xml格式或者json格式,然后需要自己根据需要获取的位置来获取信息。而在生成的HL7协议报文的时候也是需要先生成xml或json格式再进行转换。想着希望找到一个直接解析生成类然后直接用的工具。 后来我找到了这个ca.uhn.hapi,能将HL7报文直接解析成相应的类,通过调用:PipeParser.parse(message, hl7str)来解析报文,将数据填充到message类里面,其中message是工具里面的继承Message类的子类,例如:QBP_Q11、RSP_K23等。而生成HL7报文的时候又可以调用message.encode()来生成,不过需要先设置MSH头

一、依赖:

        <dependency>
            <groupId>ca.uhn.hapi</groupId>
            <artifactId>hapi-base</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>ca.uhn.hapi</groupId>
            <artifactId>hapi-structures-v24</artifactId>
            <version>2.3</version>
        </dependency>

因为我们是通过socket对接实现的,先实现socket代码

1.启动类

@SpringBootApplication
@EnableCaching
@Configuration
@EnableScheduling
@ServletComponentScan
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer {

 public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Application.class, args);
        SpringContextUtil.setApplicationContext(context);
        SocketServer demoServer = new SocketServer();
        try {
            demoServer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.SocketServer(服务端)

@Component
public class SocketServer{

    private static final Logger logger = LoggerFactory.getLogger(SocketServer.class);

    public void start() throws IOException {
        ExecutorService newCacheThreadPool = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        ServerSocket server = new ServerSocket(SocketProperties.getSocketPort());
        logger.info("socket服务端启动....端口:{}",SocketProperties.getSocketPort());
        while (true) {
            final Socket socket = server.accept();
            logger.info("开始处理请求");
            newCacheThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler(socket);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private  void handler(Socket socket) throws Exception {
        byte[] bytes = null;
        InputStream inputStream = null;
        try {
            inputStream = socket.getInputStream();
            int bufflenth = inputStream.available();
            while (bufflenth != 0) {
                bytes = new byte[bufflenth];
                //读取数据(阻塞)
                inputStream.read(bytes);
                bufflenth = inputStream.available();
            }
            String result = new String(bytes);
            result =  new String(bytes).replaceAll("\n","\r")
                    .replaceAll("\u000B","").replaceAll("amp;","").replaceAll("\u001c","");
            logger.info("请求:{}" ,result);
            TestHL7.hl7Text2Obj(result,socket);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            logger.info("socket关闭");
            try {
                inputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3.SocketClient

@Slf4j
public class SocketClient {
/**
     * 发送socket消息
     */
    public static String sendSocket(String message){
        Socket socket = null;
        String result = "";
        InputStream in = null;
        OutputStream out = null;
        try {
            socket = new Socket(SocketProperties.getSocketSendIp(),SocketProperties.getSocketSendPort());
            in = socket.getInputStream();
            out = socket.getOutputStream();
            log.info("----------开始发送 MESSAGE-----------{}",message);
            out.write(message.getBytes(SocketConstants.UTF_8));
            out.flush();
            socket.shutdownOutput();
            byte[] byteBuffer = new byte[8*1024];
            in.read(byteBuffer);
            result = new String(byteBuffer);
            log.info("Received from Server message: " + result);
            result =  result.replaceAll("\n","\r")
                    .replaceAll("\u000B","").replaceAll("amp;","").replaceAll("\u001c","");
            return result;
        }catch (Exception e){
            log.error("发送socket消息失败",e);
        }finally {
            if(socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(in!=null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(out!=null) {
               
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值