0

我正在尝试解组 json 有效负载并将所有带有页眉和页脚的记录写入文本文件中。所有记录都有| 分隔符,包括页眉和页脚。我想更换 | 与 |"(管道 + 引号)。

我在这个过程中遇到了异常。org.apache.camel.language.bean.RuntimeBeanExpressionException:调用方法失败:replace('|', '|"') on null 由于:org.apache.camel.component.bean.MethodNotFoundException:方法名称:replace ('|', '|"')

有人可以帮我解决这个问题吗?

@Component
public class EmployeePackageRoute extends RouteBuilder {

    @Autowired
    private Header header;
    
    private Trailer footer;
    
    @SuppressWarnings("deprecation")
    @Override
    public final void configure() {
        System.out.println("fetching employee details from employee api");
        try {
            
            from("jetty://http://localhost:8084/employee-package/getEmployee")
            .unmarshal().json(JsonLibrary.Jackson, RecurringPaymentResults.class)
            .process(
                    ex -> {
                        RecurringPaymentResults result = ex.getIn().getBody(RecurringPaymentResults.class);
                        List<RecurringPaymentsDeduct> employee = result.getResults();
                        ex.getOut().setBody(employee);
                        
                    })
            .to("seda:employeeFeed");
            
            final DataFormat bindy = new BindyFixedLengthDataFormat(RecurringPaymentsDeduct.class);
            
            footer = new Trailer("TRAILER",3);
            
            Map<String, Object> headerObjMap = new HashMap<String,Object>();
            headerObjMap.put(Header.class.getName(), header);
            
            Map<String, Object> footerObjMap = new HashMap<String,Object>();
            footerObjMap.put(Trailer.class.getName(), footer);
            
            from("seda:employeeFeed")
            .log("process csv")
            .process(
                    ex -> {
                                                
                        ex.getOut().setHeader(BindyFixedLengthDataFormat.CAMEL_BINDY_FIXED_LENGTH_HEADER, headerObjMap);
                        ex.getOut().setBody(ex.getIn().getBody());
                        ex.getOut().setHeader(BindyFixedLengthDataFormat.CAMEL_BINDY_FIXED_LENGTH_FOOTER, footerObjMap);
                        
                    })
            .marshal(bindy)
            .log("csv processed")
            .transform(simple("${body.replace('|', '|\"')}"))
            //.to("file://C:\\Users\\testfolder?fileName=test.csv")
            .to("file://C:\\Users\\testfolder?fileName=test.txt")
            .end();
            
                        
        } catch(Exception e) {
            System.out.println("Error occurred while processing employee data: "+e.getMessage());
            e.printStackTrace();
        }
    }
}

    @Data
    @Section(number = 2)
    @FixedLengthRecord(header = Header.class, footer = Trailer.class)
    public class RecurringPaymentsDeduct implements Serializable {
    
        private static final long serialVersionUID = 1L;
        
       @DataField(pos = 1, length = 5, delimiter = "|")
    public String RECTY;
    
    @DataField(pos = 2, length = 5, delimiter = "|")
    public String CLIID;
    
    @DataField(pos = 3, length = 5, delimiter = "|")
    public String INTCA;
    
    @DataField(pos = 4, length = 5, delimiter = "|")
    public String ORDNO;
    
    @DataField(pos = 5, length = 5, delimiter = "|")
    public String IOPER;
    
    @DataField(pos = 6, length = 5, delimiter = "|")
    public String INFTY;
    
    @DataField(pos = 7, length = 5, delimiter = "|")
    public String SUBTY;
    
    @DataField(pos = 8, length = 5, delimiter = "|")
    public String BEGDA;
    
    @DataField(pos = 9, length = 5, delimiter = "|")
    public String ENDDA;
    
    @DataField(pos = 10, length = 5, delimiter = "|")
    public String OBJPS;
    
        
    }


@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("test")
@Data
@Section(number = 1)
@FixedLengthRecord()
public class Header implements Serializable {

    private static final long serialVersionUID = 1L;

    @DataField(pos = 1, length = 5, delimiter = "|")
    private String header1;
    
    @DataField(pos = 2, length = 6, delimiter = "|")
    private String header2;
    
    @DataField(pos = 3, length = 13, delimiter = "|")
    private String header3;
    
    @DataField(pos = 4, length = 37, delimiter = "|")
    private String header4;
    
    @DataField(pos = 5, length = 15, delimiter = "|")
    private String header5;
    
    @DataField(pos = 6, length = 33, delimiter = "|")
    private String header6;
    
    @DataField(pos = 7, length = 47, delimiter = "|")
    private String header7;
    
    @DataField(pos = 8, length =  8, pattern = "YYYYMMDD", delimiter = "|")
    private String header8;
    
    @DataField(pos = 9, length = 6, delimiter = "|")
    private String header9;
    
    @DataField(pos = 10, length = 1, delimiter = "|")
    private String header10;
    
    @DataField(pos = 11, length = 2, delimiter = "|")
    private String header11;
    
    @DataField(pos = 12, length = 2, delimiter = "|")
    private String header12;
    
    @DataField(pos = 13, length = 2, delimiter = "|")
    private String header13;
}


@Section(number = 3)
@FixedLengthRecord
public class Trailer implements Serializable {

    private static final long serialVersionUID = 1L;

   @DataField(pos = 1, length = 7, delimiter = "|")
    private String trailer;
    
    @DataField(pos = 2, length = 2)
    private int count;
    
    public String getTrailer() {
        return trailer;
    }
    public void setTrailer(String trailer) {
        this.trailer = trailer;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
}

示例输出:HEADR|GVEMQG|xxxx GROUP|Tech Workplace HR Team|+61 408 188 031|itghrteam@xxxx.com|AQEMQG_20171113170000_AUMBCR_HRMD01_DUT8G2I.SAP|YYYYMMDD|HHMMSS|P|01|||

RECTY|CLIID|INTCA|ORDNO|IOPER|INFTY|SUBTY|BEGDA|ENDDA|OBJPS|SPRPS|SEQNR|EXTRA|LGART|BETRG|WAERS|ANZHL|ZEINH|ZDATE|ZFPER|ZANZL|ZEINZ|ZUORD|MODEL|UWDAT

拖车| 3

4

2 回答 2

0

正确获取字符串后,您应该替换字符。我的意思是确保在调用 replace 方法之前发生 JSON 到字符串的转换。我不知道你用来获取字符串的语法和技术,但我认为它会是……。像“simple("${body}).replace('|', '|"')”。我的意思是如果 "simple("${body})" 对应于一个字符串,那么你将能够调用替换。

于 2020-09-13T09:49:25.383 回答
0

找到了正确答案。将 body 转换为字节数组或字符串并替换 piple 符号

from(fromEndpoint)
    .convertBodyTo(byte[].class, "iso-8859-1")
    .setBody(body().regexReplaceAll("\\|", "\\|\""))
于 2020-09-16T00:14:54.403 回答