Package org.mapstruct

Annotation Type BeanMapping


  • @Target(METHOD)
    @Retention(CLASS)
    public @interface BeanMapping
    Configures the mapping between two bean types.

    Either resultType(), qualifiedBy() or nullValueMappingStrategy() must be specified.

    Example: Determining the result type

    
     // When result types have an inheritance relation, selecting either mapping method Mapping or factory method
     // BeanMapping can be become ambiguous. Parameter  resultType() can be used.
     public class FruitFactory {
         public Apple createApple() {
             return new Apple();
         }
         public Orange createOrange() {
             return new Orange();
         }
     }
     @Mapper(uses = FruitFactory.class)
     public interface FruitMapper {
         @BeanMapping(resultType = Apple.class)
         Fruit toFruit(FruitDto fruitDto);
     }
     
    
     // generates
     public class FruitMapperImpl implements FruitMapper {
          @Override
          public Fruit toFruit(FruitDto fruitDto) {
              Apple fruit = fruitFactory.createApple();
              // ...
          }
     }
     
    Author:
    Sjaak Derksen
    • Element Detail

      • resultType

        Class<?> resultType
        Specifies the result type of the factory method to be used in case several factory methods qualify.
        Returns:
        the resultType to select
        Default:
        void.class
      • qualifiedBy

        Class<? extends Annotation>[] qualifiedBy
        A qualifier can be specified to aid the selection process of a suitable factory method or filtering applicable @BeforeMapping / @AfterMapping methods. This is useful in case multiple factory method (hand written of internal) qualify and result in an 'Ambiguous factory methods' error.

        A qualifier is a custom annotation and can be placed on either a hand written mapper class or a method.

        Returns:
        the qualifiers
        See Also:
        Qualifier
        Default:
        {}
      • qualifiedByName

        String[] qualifiedByName
        Similar to qualifiedBy(), but used in combination with @Named in case no custom qualifier annotation is defined.
        Returns:
        the qualifiers
        See Also:
        Named
        Default:
        {}
      • nullValueCheckStrategy

        NullValueCheckStrategy nullValueCheckStrategy
        Determines when to include a null check on the source property value of a bean mapping. Can be overridden by the one on MapperConfig, Mapper or Mapping.
        Returns:
        strategy how to do null checking
        Default:
        org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION
      • subclassExhaustiveStrategy

        SubclassExhaustiveStrategy subclassExhaustiveStrategy
        Determines how to handle missing implementation for super classes when using the SubclassMapping. Overrides the setting on MapperConfig and Mapper.
        Returns:
        strategy to handle missing implementation combined with SubclassMappings.
        Since:
        1.5
        Default:
        org.mapstruct.SubclassExhaustiveStrategy.COMPILE_ERROR
      • ignoreByDefault

        boolean ignoreByDefault
        Default ignore all mappings. All mappings have to be defined manually. No automatic mapping will take place. No warning will be issued on missing source or target properties.
        Returns:
        The ignore strategy (default false).
        Since:
        1.3
        Default:
        false
      • builder

        Builder builder
        The information that should be used for the builder mappings. This can be used to define custom build methods for the builder strategy that one uses. If no builder is defined the builder given via MapperConfig.builder() or Mapper.builder() will be applied.

        NOTE: In case no builder is defined here, in Mapper or MapperConfig and there is a single build method, then that method would be used.

        If the builder is defined and there is a single method that does not match the name of the finisher than a compile error will occurs

        Returns:
        the builder information for the method level
        Since:
        1.3
        Default:
        @org.mapstruct.Builder