首页 文章

HAPI HL7验证会引发异常

提问于
浏览
0

我正在开发一个我打算用于HL7消息验证的Java endpoints . 我有一个运行的基本应用程序,它使用标准HAPI HL7验证示例的变体 . 如果我传入有效消息,我会收到“成功”回复 . 如果我传入无效消息,我仍然会收到“成功”回复 .

我得到错误响应的唯一方法是HL7格式错误并且PipeParser抛出异常 . 在这种情况下,它会陷入catch块 .

我想看到的是,如果我传入一条无效的消息,它实际上已经验证并返回所有验证错误 . 但我实际上没有看到任何验证 . 它解析或崩溃试图解析 .

我在这里错过了什么?

HapiContext context = new DefaultHapiContext();

    ValidationContext validationContext =  ValidationContextFactory.defaultValidation();
    context.setValidationContext(validationContext);


    try
    {
        context.getParserConfiguration().setUnexpectedSegmentBehaviour(UnexpectedSegmentBehaviourEnum.THROW_HL7_EXCEPTION);

        Message messageValidationResults = context.getPipeParser().parse(hl7Message);

        SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(context);
        handler.setMinimumSeverityToCollect(Severity.INFO);

        Validator<Boolean> validator = context.getMessageValidator();

        if (!validator.validate(messageValidationResults, handler))
        {
            if (handler.getExceptions().size() == 0)
            {
                hl7ValidationResult = "SUCCESS - Message Validated Successfully";
            }
            else
            {
                hl7ValidationResult = "ERROR - Found " + handler.getExceptions().size() + " problems\n\n";
                for (Exception e : handler.getExceptions())
                {
                    hl7ValidationResult += (e.getClass().getSimpleName() + " - " + e.getMessage()) + "\n";
                }
            }

        }
    }
    catch (Exception e)
    {
        hl7ValidationResult = "ERROR - " + e.getMessage();

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String sStackTrace = sw.toString();

        hl7ValidationResult += "\n\n" + sStackTrace;
    }

1 回答

  • 1

    如果你认为不正确,请忽略答案,我停止使用HL7,但是,看看我的旧项目,我找到了这个,也许它可以帮助你找到问题的解决方案:

    {
    
        DefaultValidationBuilder builder = new DefaultValidationBuilder() {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            protected void configure() {
                super.configure();
                forVersion(Version.V26);
            }
    
        };
    
        HapiContext context = new DefaultHapiContext();
        context.setValidationRuleBuilder(builder);
        PipeParser hapiParser = context.getPipeParser();
    
        try {
    
            hapiParser.parse(hl7Message);
    
        } catch (ca.uhn.hl7v2.HL7Exception e) {
    
            // String error, String language, String requisitionNumber, String controlId, String processinId, String senderApplication, String senderFacility
            errors.add(new HL7ValidationError(
                "HAPI Validator error found: " + e.getMessage(), 
                extractor.accessPatientDirectly().getLanguage(), 
                extractor.accessPatientDirectly().getRequisitionNumber(), 
                extractor.accessPatientDirectly().getControlID(), 
                "", 
                extractor.accessPatientDirectly().getSenderApplication(), 
                extractor.accessPatientDirectly().getSenderFacility())
            );  
            log.debug("HAPI Validator error found: " + e.getMessage()); 
        }
    
        try {
            context.close();
        }
        catch (Exception ex) {
            log.debug("Unable to close HapiContext(): " + ex.getMessage());
        }
    
    }
    

    基本上我使用 hapiParser.parse(hl7Message); 并捕获HL7Exception

相关问题