简体   繁体   English

以声明方式将 JavaObject 映射到 Javabean

[英]Mapping JavaObject to Javabeans in declarative way

Edit1编辑1

I am not sure if the title is best for the problem so if any have some more orinted title please suggest我不确定标题是否最适合这个问题,所以如果有更多的标题,请建议

i am trying my hands on camel where i have to fetch some csv file from a file system and needs to convert it to xml format and place it on some other system我正在尝试使用骆驼,我必须从文件系统中获取一些 csv 文件,并且需要将其转换为 xml 格式并将其放在其他系统上

i am using camel for this and here is my sample POC code我为此使用骆驼,这是我的示例 POC 代码

import org.apache.camel.CamelContext;

import org.apache.camel.Exchange;

import org.apache.camel.Message;

import org.apache.camel.Processor;

import org.apache.camel.builder.RouteBuilder;

import org.apache.camel.impl.DefaultCamelContext;
import com.poc.convertor.CSVConverterBean;

  public class TestPOC {

           public static void main(String args[]) throws Exception {
           CamelContext context = new DefaultCamelContext();
           context.addRoutes(new RouteBuilder() {

                  public void configure() {
                          from("file:data/csv?noop=true").unmarshal().csv().bean(new CSVConverterBean(),"processCSVInvoice").to("file:data/csvoutput?fileName=test.xml").marshal("jaxb");


                  }

              });
               context.start();
               Thread.sleep(1000);
               context.stop();
          }

}

In this approach camel csv unmarshaller will covert the csv file in to java list List<List<String>> i have written a java converter CSVConverterBean which will iterate the list and set the values in the respective java objects being generated by jaxb 2.x, final object is being marshaled in to xml and file is being saved. In this approach camel csv unmarshaller will covert the csv file in to java list List<List<String>> i have written a java converter CSVConverterBean which will iterate the list and set the values in the respective java objects being generated by jaxb 2.x ,最终的 object 正在编组到 xml 并正在保存文件。

Everything is being working properly with only one issue, if in future there arises any request to change mapping we need to do the modification in CSVConverterBean and than this java file needs to be recompiled and need to be redistributed which we want to avoid.一切正常,只有一个问题,如果将来出现任何更改映射的请求,我们需要在CSVConverterBean中进行修改,然后这个 java 文件需要重新编译并且需要重新分发,这是我们想要避免的。

my question is, Is there any way by which we can map the values from the java List being given by Camel to the respective java classes being generated by the JaxB so that the need to recompile java code can be avooided. my question is, Is there any way by which we can map the values from the java List being given by Camel to the respective java classes being generated by the JaxB so that the need to recompile java code can be avooided.

You can provide a "from-to" kind of configuration file to map the columns of your CSV data with your Bean properties, and code an algorithm to read that file and process the convertion.您可以向 map 提供带有 Bean 属性的 CSV 数据的列的“从到”类型的配置文件,并编写一个算法来读取该文件并处理转换。

You could do with a.properties file:您可以使用 a.properties 文件:

# mapping.properties
column0=propertyOne
column1=propertyTwo

For each column in your CSV, you get the value from the property file and find which property you should set the value on.对于 CSV 中的每一列,您从属性文件中获取值并找到您应该在哪个属性上设置值。

int columnIndex = 0;
for(String column : csvColumns) {
    String property = findProperty(columnIndex);
    reflectionUtil.setValue(object, property, column);
    columnIndex++;
}

This may give you some hint.这可能会给你一些提示。

Whenever your data changes, you will only need to change the property file, not the class.每当您的数据更改时,您只需要更改属性文件,而不是 class。

Use Bruno's idea, but read the property names from the header row in the csv file.使用 Bruno 的想法,但从 csv 文件中的 header 行中读取属性名称。

I have solved this problem using dom4j.camel gave me back csv as list> and firstly i read the headers and than made these headers and the XML tags and the values as there respected values on run time.我已经使用 dom4j.camel 解决了这个问题,将 csv 作为 list> 返回给我,首先我读取了标题,然后制作了这些标题和 XML 标签和值,因为它们在运行时尊重了值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM