How to Work with Orika Spring Boot Starter

Dec 20, 2023 | Programming

If you’re looking to streamline your bean mapping in a Spring Boot application, the Orika Spring Boot Starter is a great tool to utilize. In this blog, we’ll guide you step-by-step through its features and usage to make your integration smooth and easy.

Features

  • Manages MapperFacade in the application context, making it easy to inject into your code.
  • Provides an interface to configure MapperFactory.
  • Allows configuration of MapperFactoryBuilder.
  • Includes configuration properties to fine-tune your MapperFactoryBuilder to suit your application’s needs.

Dependencies

To use Orika Spring Boot Starter, ensure the following dependencies are included:

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

Please remember to include the required JVM option:

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

Usage

1. Adding the Dependency

To start using Orika, add the following dependency to your Maven project:

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

2. Injecting the MapperFacade

The MapperFacade is managed within the application context. You can easily inject it into your code. Here’s how you can achieve that in Java:

import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;

@Autowired
private MapperFacade orikaMapperFacade;

3. Mapping Your Beans

You can map your beans using the MapperFacade. Let’s say you want to transfer data from a PersonSource to PersonDestination. Here’s the analogy:

Imagine you have two cities, Source City and Destination City, and you need to send a package from one to the other while changing its label. Here’s how you’d do that using Orika:

PersonSource src = new PersonSource("John", "Smith", 23);
System.out.println(src); // Prints: PersonSource(firstName=John, lastName=Smith, age=23)

PersonDestination dest = orikaMapperFacade.map(src, PersonDestination.class);
System.out.println(dest); // Prints: PersonDestination(givenName=John, sirName=Smith, age=23)

4. MapperFactory Configuration

You can create your own instance of OrikaMapperFactoryConfigurer in your application context. Here is how you can configure it:

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();
    }
}

5. MapperFactoryBuilder Configuration

You can also create an instance of OrikaMapperFactoryBuilderConfigurer. Here’s an example:

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 here
    }
}

6. Configuration Properties

You can configure various properties in your application.yml or application.properties. Here are some examples:

orika:
  enabled: true
  use-builtin-converters: true
  use-auto-mapping: true
  map-nulls: true
  dump-state-on-exception: false
  favor-extension: false
  capture-field-context: false

Troubleshooting

If you encounter any issues while using Orika Spring Boot Starter, consider checking the following:

  • Ensure that your Maven dependencies are correctly set up.
  • Verify that your versions of Java, Spring Boot, and Kotlin match the requirements.
  • Look for any errors in your configuration files that may affect your MapperFactory setup.
  • Check the logs for specific error messages related to bean mapping.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox