Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll see how to map the values of an enum to those of another enum with MapStruct. We’ll also learn how to throw an Exception when there’s no corresponding value in the other enum.

2. The MapStruct Library

MapStruct is a code generation tool that simplifies Java Beans mapping. The latest version of the MapStruct library can be found in the Maven Central Repository.

Let’s add the dependency to our pom.xml:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.6.0.Beta1</version> 
</dependency>

Additionally, we need to add annotationProcessorPaths to the maven-compiler-plugin plugin in order to generate automatically the methods inside the project’s target folder:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct</artifactId>
                <version>1.6.0.Beta1</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

3. Introduction to the Problem

First, let’s create our source enum. We’ll name it InputLevel, and it’ll have three possible values: HIGH, MEDIUM, and LOW:

enum InputLevel {

    LOW, MEDIUM, HIGH

}

We can now add the target enum. This one only contains two values HIGH and LOW:

enum OutputLevel {

    LOW, HIGH

}

We aim at converting an InputLevel to an OutputLevel. For instance, the input InputLevel.LOW gives us OutputLevel.LOW. However, there’s no matching value for MEDIUM. As a consequence, we want to throw an Exception in this case.

4. Throw When Source Has No Corresponding Target

We’ll use the Mapper annotation and create a Mapper interface. Since version 1.5.0.Beta1 of the MapStruct library, we can use the @ValueMapping annotation to achieve our goal:

@Mapper
interface LevelMapper {

    @ValueMapping(source = MappingConstants.ANY_REMAINING, target = MappingConstants.THROW_EXCEPTION)
    OutputLevel inputLevelToOutputLevel(InputLevel inputLevel);

}

As we can see, we configured the annotation so that mapping any value in the source without a corresponding target would result in an Exception.

Let’s now quickly check that the method correctly returns OutputLevel.HIGH when it’s given InputLevel.HIGH:

LevelMapper levelMapper = Mappers.getMapper(LevelMapper.class);

@Test
void givenHighInputLevel_WhenInputLevelToOutputLevel_ThenHighOutputLevel() {
    assertEquals(OutputLevel.HIGH, levelMapper.inputLevelToOutputLevel(InputLevel.HIGH));
}

Finally, we’ll confirm that when we try to convert InputLevel.MEDIUM into an OutputLevel, an Exception is thrown. Concretely, it throws an IllegalArgumentException:

@Test
void givenMediumInputLevel_WhenInputLevelToOutputLevel_ThenThrows() {
    assertThrows(IllegalArgumentException.class, () -> levelMapper.inputLevelToOutputLevel(InputLevel.MEDIUM));
}

5. Conclusion

In this article, we used the MapStruct library to map values from a source enum toward a target enum. Moreover, we configured our mapper to throw an Exception if the source value has no match in the target enum.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.

 

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW