如何跳过Spring Batch Job中jaxb集合中包含的单个jaxb元素验证? [英] How to skip a single jaxb element validation contained in a jaxb collection in Spring Batch Job?

查看:61
本文介绍了如何跳过Spring Batch Job中jaxb集合中包含的单个jaxb元素验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户在一个xml文件中发送了5万个客户.我使用 Spring Batch的JaxBMarshaller 并在Spring Batch作业中运行.

Client sends 50k customers in an xml file. I use Spring Batch's JaxBMarshaller and run in a Spring Batch job.

Spring批处理作业获取文件,处理和写入.

Spring batch job gets a file, processes, and writes.

问题是,它是用jaxb进行的全部或全部验证.如果我有5万个对象,而其中只有2个对象未通过验证,那么我仍然需要49,998个对象来进行业务处理.

Problem is, it's ALL or NONE validation with jaxb. If I have 50k objects and only 2 of them fail validation, I still need 49,998 objects to be processed by business.

有一个类javax.xml.bind.ValidationEventHandler;您可以将其设置为JaxBMarshaller,但它只会返回true或false,并且不提供对封送对象的访问.

There's a class, javax.xml.bind.ValidationEventHandler; you can set it to JaxBMarshaller but it only returns true or false and provides no access to the object being marshaled.

我还添加了块读取器;错误仍然抛出.

I also added on the chunk Reader; error still throws.

示例架构:

<xs:element name="CustomerLists">
   <xs:complexType>
       <xs:sequence>
           <xs:element name="Customer" maxOccurs="unbounded" type="Customer"/>
       </xs:sequence>
   </xs:complexType>
</xs:element>

样本Xml:

<a:CustomerLists xmlns:a="http://foo.com">

    <a:Customer>
        ...
    ...
        ...
    </a:Customer>

    <a:Customer>
        ...
    ...
    ...
    </a:Customer>

    <a:Customer>
        ...
    ...
    ...
    </a:Customer>

</a:CustomerLists>

建议?

推荐答案

javax.xml.bind.ValidationEventHandler是与JAXB一起使用的正确机制.您可以通过ValidationEvent

The javax.xml.bind.ValidationEventHandler is the correct mechanism to use with JAXB. You can access the problematic object for an unmarshal operation through the ValidationEvent:

package blog.jaxb.validation;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

public class MyValidationEventHandler implements ValidationEventHandler {

    public boolean handleEvent(ValidationEvent event) {
        System.out.println("\nEVENT");
        System.out.println("SEVERITY:  " + event.getSeverity());
        System.out.println("MESSAGE:  " + event.getMessage());
        System.out.println("LINKED EXCEPTION:  " + event.getLinkedException());
        System.out.println("LOCATOR");
        System.out.println("    OBJECT:  " + event.getLocator().getObject());
        return true;
    }


}

更多信息

这篇关于如何跳过Spring Batch Job中jaxb集合中包含的单个jaxb元素验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆