Skip to content

akkinoc/orika-spring-boot-starter

orika-spring-boot-starter

maven central badge javadoc badge release badge build badge codecov badge license badge sponsor badge

Spring Boot Starter for Orika.

⚠️ Please note that Orika has not been maintained for a long time (orika-mapper/orika#372).

Features

  • Manages MapperFacade in the application context and makes it injectable into your code.
  • Provides an interface to configure MapperFactory.
  • Provides an interface to configure MapperFactoryBuilder.
  • Provides configuration properties to configure MapperFactoryBuilder.

Dependencies

Depends on:

  • Java 17 or 21
  • Kotlin 1.9
  • Spring Boot 3.3
  • Orika 1.5

The following JVM option is required (orika-mapper/orika#377).

--add-opens java.base/java.lang=ALL-UNNAMED

Usage

Adding the Dependency

The artifact is published on Maven Central Repository. If you are using Maven, add the following dependency.

<dependency>
    <groupId>dev.akkinoc.spring.boot</groupId>
    <artifactId>orika-spring-boot-starter</artifactId>
    <version>${orika-spring-boot-starter.version}</version>
</dependency>

Injecting the MapperFacade

The MapperFacade is managed in the application context. Inject the MapperFacade into your code.

For example in Java:

import ma.glasnost.orika.MapperFacade;
@Autowired
private MapperFacade orikaMapperFacade;

Mapping Your Beans

Map your beans using the MapperFacade.

For example in Java:

// Maps from PersonSource to PersonDestination
PersonSource src = new PersonSource("John", "Smith", 23);
System.out.println(src);   // => "PersonSource(firstName=John, lastName=Smith, age=23)"
PersonDestination dest = orikaMapperFacade.map(src, PersonDestination.class);
System.out.println(dest);  // => "PersonDestination(givenName=John, sirName=Smith, age=23)"

MapperFactory Configuration

If you need to configure the MapperFactory, create an instance of OrikaMapperFactoryConfigurer in the application context. The OrikaMapperFactoryConfigurer components are auto-detected and the "configure" method is called.

For example in Java:

import dev.akkinoc.spring.boot.orika.OrikaMapperFactoryConfigurer;
import ma.glasnost.orika.MapperFactory;

@Component
public class PersonMapping implements OrikaMapperFactoryConfigurer {
    @Override
    public void configure(MapperFactory orikaMapperFactory) {
        orikaMapperFactory.classMap(PersonSource.class, PersonDestination.class)
                .field("firstName", "givenName")
                .field("lastName", "sirName")
                .byDefault()
                .register();
    }
}

See also the Orika User Guide:

MapperFactoryBuilder Configuration

If you need to configure the MapperFactoryBuilder, create an instance of OrikaMapperFactoryBuilderConfigurer in the application context. The OrikaMapperFactoryBuilderConfigurer components are auto-detected and the "configure" method is called.

For example in Java:

import dev.akkinoc.spring.boot.orika.OrikaMapperFactoryBuilderConfigurer;
import ma.glasnost.orika.impl.DefaultMapperFactory.MapperFactoryBuilder;

@Component
public class OrikaConfiguration implements OrikaMapperFactoryBuilderConfigurer {
    @Override
    public void configure(MapperFactoryBuilder<?, ?> orikaMapperFactoryBuilder) {
        // Your configuration codes.
    }
}

See also the Orika User Guide:

Configuration Properties

Provides the following configuration properties. These can be configured by your "application.yml", "application.properties", etc.

# The configuration properties for Orika.
orika:
  # Whether to enable auto-configuration.
  # Defaults to true.
  enabled: true
  # Whether to use built-in converters.
  # See also MapperFactoryBuilder.useBuiltinConverters.
  # By default, follows Orika's behavior.
  use-builtin-converters: true
  # Whether to use auto-mapping.
  # See also MapperFactoryBuilder.useAutoMapping.
  # By default, follows Orika's behavior.
  use-auto-mapping: true
  # Whether to map nulls.
  # See also MapperFactoryBuilder.mapNulls.
  # By default, follows Orika's behavior.
  map-nulls: true
  # Whether to dump the current state of the mapping infrastructure objects
  # upon occurrence of an exception while mapping.
  # See also MapperFactoryBuilder.dumpStateOnException.
  # By default, follows Orika's behavior.
  dump-state-on-exception: false
  # Whether to favor extension by default in registered class-maps.
  # See also MapperFactoryBuilder.favorExtension.
  # By default, follows Orika's behavior.
  favor-extension: false
  # Whether full field context should be captured.
  # See also MapperFactoryBuilder.captureFieldContext.
  # By default, follows Orika's behavior.
  capture-field-context: false

API Reference

Please refer to the Javadoc.

Release Notes

Please refer to the Releases page.

License

Licensed under the Apache License, Version 2.0.

Support the Project

If this project is useful to you, I appreciate giving a ⭐ star to this repository. I would also appreciate if you would consider 💖 sponsoring as well. Your support is my biggest motive force. Thanks ✨