MapStruct - Using numberFormat



MapStruct handles conversion of numbers to String in required format seamlessly. We can pass the required format as numberFormat during @Mapping annotation. For example, consider a case where an amount stored in numbers is to be shown in currency format.

  • Source − Entity has price as 350.

  • Target − Model to show price as $350.00.

  • numberFormat − Use format $#.00

Example

Open project mapping as updated in Mapping Implicit Type Conversions chapter in Eclipse.

Create CarEntity.java with following code −

CarEntity.java

package com.tutorialspoint.entity;

public class CarEntity {
   private int id;
   private double price;
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public double getPrice() {
      return price;
   }
   public void setPrice(double price) {
      this.price = price;
   }
}

Create Car.java with following code −

Car.java

package com.tutorialspoint.model;

public class Car {
   private int id;
   private String price;
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getPrice() {
      return price;
   }
   public void setPrice(String price) {
      this.price = price;
   }
}

Create CarMapper.java with following code −

CarMapper.java

package com.tutorialspoint.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.tutorialspoint.entity.CarEntity;
import com.tutorialspoint.model.Car;

@Mapper
public interface CarMapper {
   @Mapping(source = "price", target = "price", numberFormat = "$#.00")
   Car getModelFromEntity(CarEntity carEntity);
}

Create CarMapperTest.java with following code −

CarMapperTest.java

package com.tutorialspoint.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.entity.CarEntity;
import com.tutorialspoint.mapper.CarMapper;
import com.tutorialspoint.model.Car;

public class CarMapperTest {
   private CarMapper carMapper = Mappers.getMapper(CarMapper.class);
   
   @Test
   public void testEntityToModel() {
      CarEntity entity = new CarEntity();
      entity.setPrice(345000);
      entity.setId(1);
      Car model = carMapper.getModelFromEntity(entity);
      assertEquals(model.getPrice(), "$345000.00");
      assertEquals(entity.getId(), model.getId());
   }
}

Run the following command to test the mappings.

mvn clean test

Output

Once command is successful. Verify the output.

mvn clean test
[INFO] Scanning for projects...
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping ---
[INFO] Surefire report directory: \mvn\mapping\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialspoint.mapping.CarMapperTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec
Running com.tutorialspoint.mapping.DeliveryAddressMapperTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Running com.tutorialspoint.mapping.StudentMapperTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
...
Advertisements