Handling of JAXB lists without setters

1,117 views
Skip to first unread message

Ernesto Diaz

unread,
Apr 27, 2017, 9:29:55 AM4/27/17
to modelmapper
Hi guys,

Sorry if I am missing something.
JAXB doesn't generate setters for lists, as stated in the specification. We have to use getObjectList().add(object) to add a new object.
I am struggling finding a way to map lists, or the content of the lists inside PropertyMaps, as they are always looking for setters (as well as I know).
I was trying to create a converter before, but I have to use the PropertyMap because the source and the destination paths are completely different.

I will appreciate of you give me a direction...

Thanks in advance,
Ernesto Diaz
(I am using the latest stable version)

Chun Han Hsiao

unread,
Apr 27, 2017, 11:47:24 PM4/27/17
to modelmapper
Hi Ernesto,

PropertyMap provides a methodology that you can use destination's getter to represent its property.
Like map(source.getSome(), destination.getOther());
But without setter, you need to enable field matching and set field access level.
Please try like following code snippet.

@Test
public class NoSetterTest1 extends AbstractTest {

  static class Source {
    private String str;

    public Source(String str) {
      this.str = str;
    }

    public String getStr() {
      return str;
    }

    public void setStr(String str) {
      this.str = str;
    }
  }

  static class Dest {
    private List<String> list;

    public List<String> getList() {
      return list;
    }
  }

  static class SplitConverter implements Converter<String, List<String>> {
    public List<String> convert(MappingContext<String, List<String>> context) {
      return Arrays.asList(context.getSource().split(","));
    }
  }

  public void shouldMap() {
    modelMapper.getConfiguration()
        .setFieldMatchingEnabled(true)
        .setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);

    modelMapper.addMappings(new PropertyMap<Source, Dest>() {
      @Override
      protected void configure() {
        using(new SplitConverter()).map(source.getStr(), destination.getList());
      }
    });

    modelMapper.validate();

    assertEquals(modelMapper.map(new Source("a,b,c"), Dest.class).list,
        Arrays.asList("a", "b", "c"));
  }
}

Regards,
Chun Han


Ernesto Diaz於 2017年4月27日星期四 UTC+8下午9時29分55秒寫道:

Jonathan Halterman

unread,
Apr 28, 2017, 12:12:42 AM4/28/17
to model...@googlegroups.com
I don't know how many JAXB users there are, but if it's possible to solve this problem in a reusable way, it could make for a good integration.

--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ernesto Diaz

unread,
Apr 28, 2017, 6:29:22 AM4/28/17
to modelmapper
Hi Chun,

First of all, thank you very much for your quick answer.
I performed the Unit test below and it worked fine for me. Then I replaces your classed with the ones that were generated and it worked well as well, so so far so good until now :)
I am happy I can continue with the introduction of your framework in the project :-)

Thank you again!
Ernesto Diaz
Reply all
Reply to author
Forward
0 new messages