2

I am trying to map a SOAP request to a Java POJO using org.modelmapper.ModelMapper

But, its not working due to the structure of the source, it has a list of strings ....

Below is the snippet for the 3 main components source soap message, destination DTO Java POJO and the service endpoint mapping logic.

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "EventRequestBodyType", propOrder = {
        "content"
    })
    public class EventRequestBodyType {

        @XmlElementRefs({
            @XmlElementRef(name = "field1", namespace = "http://x.y.z.event", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "field2", namespace = "http://x.y.z.event", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "field3", namespace = "http://x.y.z.event", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "field4", namespace = "http://x.y.z.event", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "field5", namespace = "http://x.y.z.event", type = JAXBElement.class, required = false)
        })
        protected List<JAXBElement<? extends Serializable>> content;

        /**
         * Gets the rest of the content model. 
         * 
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * {@link JAXBElement }{@code <}{@link BigInteger }{@code >}
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * 
         * 
         */
        public List<JAXBElement<? extends Serializable>> getContent() {
            if (content == null) {
                content = new ArrayList<JAXBElement<? extends Serializable>>();
            }
            return this.content;
        }

    }

This is the POJO (What i am trying to map to)

import lombok.Data;

@Data
public class EventDTO {        
    private String field1;        
    private String field2;
    private String field3;
    private String field4;
    private String field5;
}

Below is the code snippet in my service, doing the mapping:

if(null!= soapEventRequest.getRequestBody()) {    
    EventRequestBodyType eventRequestBodyType =
                            soapEventRequest.getRequestBody();        
    EventDTO event  = modelMapper.map(eventRequestBodyType,EventDTO.class);
    eventBody.setEventRegisterAssessment(event);           
}

How do I map my EventDTO to the 5 fields returned in the EventRequestBodyType.getContent(). Any ideas will be welcomed. I donor have to use RoleMapper library, any approach or suggestions is welcomed. Thanks in advance

pirho
  • 11,565
  • 12
  • 43
  • 70
Mega
  • 1,304
  • 5
  • 22
  • 37

1 Answers1

0

Using ModelMapper might not give you any big benefit in this case - meaning that you could do this mapping easily without it also - but in case you want to use it for convention or if someone is not able to intercept the process before ModelMapper tries to map I present one option.

With ModelMapper there is an interface org.modelmapper.Converter that you can implement to perform more complex mappings.

I have made some assumptions and this might not suit directly your needs but the implementation could look something like:

public class MyConverter implements Converter<EventRequestBodyType, EventDTO> {

    public EventDTO convert(MappingContext<EventRequestBodyType, EventDTO> context) {
        List<JAXBElement<? extends Serializable>> l = context.getSource().getContent();
        final EventDTO dest = new EventDTO();
        l.stream().forEach(src -> setFieldvalue(src, dest));
        return dest;
    }

    private void setFieldvalue(JAXBElement<? extends Serializable> src, EventDTO dest)
            throws RuntimeException {
        try {
            // Just an example how the corresponding field could be determined, 
            // maybe you need to do some other tricks also with QName
            Field f = EventDTO.class.getDeclaredField(src.getName().toString());
            f.setAccessible(true);
            // Here i assume that value can be just a String
            f.set(dest, src.getValue().toString());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            // Wrapping any checked exception to unchecked for lambda
            throw new RuntimeException(e);
        }
    }

}

Then it is only to tell ModelMapper to use it:

ModelMapper mm = new ModelMapper();
mm.addConverter(new MyConverter());
EventDTO event  = modelMapper.map(eventRequestBodyType ,EventDTO.class);
pirho
  • 11,565
  • 12
  • 43
  • 70