XStreamDataFormat создает ссылку на объект при преобразовании в xml с использованием apache верблюд Бинди - PullRequest
0 голосов
/ 06 января 2020

Я хочу создать два раздела книги в тегах автора. Я использую apache верблюжий бинди, чтобы сделать преобразование. Но я смог разобрать как book, так и bookOne, когда дело дошло до того, что он генерировал только поля объекта книги, где в качестве bookone он возвращает ссылку на книгу. подробности см. ниже.

Автор. java

@Data
@CsvRecord(separator="," , skipField = true)
public class Author {

    @DataField(pos = 1)
    private String firstName;

    @DataField(pos = 2)
    private String lastName;

    @Link
    private Book book;

    @Link
    private Book bookOne;

    @DataField(pos = 5)
    private String Age;
}

Книга. java

@Data
@Link
@CsvRecord(separator = ",")
public class Book {

    @DataField(pos = 3)
    private String title;

    @DataField(pos = 4)
    private String year;
}

CamelConverter. java

 public void addRoutesToCamelContext(CamelContext context) throws Exception {

        context.addRoutes(new RouteBuilder() {

            public void configure() {
                try {
                    DataFormat bindyFixed = new BindyCsvDataFormat(Author.class);

                    XmlFriendlyNameCoder nameCoder = new XmlFriendlyNameCoder("_-", "_");
                    Dom4JDriver myCustomDriver = new Dom4JDriver(nameCoder);

                      XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
                      xStreamDataFormat.setAliases(Collections.singletonMap("Author", Author.class.getCanonicalName()));
                      xStreamDataFormat.setXstreamDriver(myCustomDriver);


                    from(SOURCE_INPUT_PATH).
                            split().tokenize(System.lineSeparator()).
                            unmarshal(bindyFixed).log("Unmarshaling ${body}").
                            marshal(xStreamDataFormat).
                            to(SOURCE_OUTPUT_PATH);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

Источник входного сигнала:

Claus,Ibsen,Camel in Action 1,2010,35
Claus,Ibsen,Camel in Action 2,2012,35
Claus,Ibsen,Camel in Action 3,2013,35
Claus,Ibsen,Camel in Action 4,2014,35

Выход:

<?xml version="1.0" encoding="UTF-8"?>

<Author>
  <firstName>Claus</firstName>
  <lastName>Ibsen</lastName>
  <book>
    <title>Camel in Action 1</title>
    <year>2010</year>
  </book>
  <bookOne reference="../book"/>
  <Age>35</Age>
</Author>

Ожидаемый результат:

<?xml version="1.0" encoding="UTF-8"?>

<Author>
  <firstName>Claus</firstName>
  <lastName>Ibsen</lastName>
  <book>
    <title>Camel in Action 1</title>
    <year>2010</year>
  </book>
  <book>
    <title>Camel in Action 1</title>
    <year>2010</year>
  </book>
  <Age>35</Age>
</Author>

Журналы:

2020-01-05 23:12:55.342  INFO 38930 --- [ - file://inbox] route1                                   : Unmarshaling Author(firstName=Claus, lastName=Ibsen, book=Book(title=Camel in Action 1, year=2010), bookOne=Book(title=Camel in Action 1, year=2010), Age=35)
2020-01-05 23:12:55.362  INFO 38930 --- [ - file://inbox] route1                                   : Unmarshaling Author(firstName=Claus, lastName=Ibsen, book=Book(title=Camel in Action 2, year=2012), bookOne=Book(title=Camel in Action 2, year=2012), Age=35)
2020-01-05 23:12:55.364  INFO 38930 --- [ - file://inbox] route1                                   : Unmarshaling Author(firstName=Claus, lastName=Ibsen, book=Book(title=Camel in Action 3, year=2013), bookOne=Book(title=Camel in Action 3, year=2013), Age=35)
2020-01-05 23:12:55.365  INFO 38930 --- [ - file://inbox] route1                                   : Unmarshaling Author(firstName=Claus, lastName=Ibsen, book=Book(title=Camel in Action 4, year=2014), bookOne=Book(title=Camel in Action 4, year=2014), Age=35)
...