Sunday 5 September 2021

MapStruts

It's always tough for me to set the values for the POJO classes. If the POJO going to be complex it will kill my whole day. Hence I was googling and found some solutions to this can be seen here


In this post, we are going to see about the mapstruts.


What is Mapstruts?


MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach.


The generated mapping code uses plain method invocations and thus is fast, type-safe, and easy to understand.


hence by using Mapstruts not only simplifies our work of setting the POJO values also, removes the tight bonding between the code and mappings.


Also, this can be used in dto types, conversion of an object from one type to another where most of the properties remain same then we can use this mapstruts to achieve this.


The following steps are used for mapping.


Step:1 Maven dependency


<properties>

<java.version>1.8</java.version>

<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>

<m2e.apt.activation>jdt_apt</m2e.apt.activation>

</properties>


<dependency>

<groupId>org.mapstruct</groupId>

<artifactId>mapstruct</artifactId>

<version>${org.mapstruct.version}</version>

</dependency>


<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>1.8</source> <!-- depending on your project -->

<target>1.8</target> <!-- depending on your project -->

<annotationProcessorPaths>

<path>

<groupId>org.mapstruct</groupId>

<artifactId>mapstruct-processor</artifactId>

<version>${org.mapstruct.version}</version>

</path>

<!-- other annotation processors -->

</annotationProcessorPaths>

</configuration>

</plugin>

</plugins>

Step:2 Create Interface


package com.greet.user.mapper;


import org.mapstruct.DecoratedWith;

import org.mapstruct.Mapper;

import org.mapstruct.Mapping;

import org.mapstruct.Mappings;

import org.mapstruct.factory.Mappers;


import com.greet.user.GreetingPojo;

import com.greet.user.MessagePojo;

import com.greet.user.decorator.GreetDecorator;


@Mapper

@DecoratedWith(GreetDecorator.class)

public interface GreetMapper {


GreetMapper INSTANCE = Mappers.getMapper(GreetMapper.class);


@Mappings({ @Mapping(source = "message", target = "welcomeMessage"),

@Mapping(target = "greetings", constant = "I am From Mapstruts") })

GreetingPojo msgtoGreet(MessagePojo msgPojo);


}


@Mapper defines the mapper class, @Mappings used to map the values from the source object to target object. Then create a method msgtoGreet which converts  MessagePojo to GreetingPojo

object.


source are the properties from the MessagePojo and target is the properties from the GreetingPojo object.


Step:3 Create a decorator class


In case the objects need to make some changes we can use this decorator class to make those changes.


package com.greet.user.decorator;


import org.springframework.beans.factory.annotation.Autowired;


import com.greet.user.GreetingPojo;

import com.greet.user.MessagePojo;

import com.greet.user.mapper.GreetMapper;


public abstract class GreetDecorator implements GreetMapper {


@Autowired

private GreetMapper delegate;


public GreetDecorator(GreetMapper delegate) {

this.delegate = delegate;

}


@Override

public GreetingPojo msgtoGreet(MessagePojo msgPojo) {

GreetingPojo dto = delegate.msgtoGreet(msgPojo);

//add manipulations to the object here.

return dto;

}


}


Stpe:4 Invoking from the class.


call this where you have to convert the object.


GreetingPojo targetObj = GreetMapper.INSTANCE.msgtoGreet(msg);


This will convert the MessagePojo POJO to the GreetingPojo object.


Happy Learning!!!!


No comments:
Write comments