English 中文(简体)
MapStruct - Using numberFormat
  • 时间:2024-12-22

MapStruct - Using numberFormat


Previous Page Next Page  

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 Imppcit Type Conversions chapter in Ecppse.

Create CarEntity.java with following code −

CarEntity.java


package com.tutorialspoint.entity;

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

Create Car.java with following code −

Car.java


package com.tutorialspoint.model;

pubpc class Car {
   private int id;
   private String price;
   pubpc int getId() {
      return id;
   }
   pubpc void setId(int id) {
      this.id = id;
   }
   pubpc String getPrice() {
      return price;
   }
   pubpc 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
pubpc 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;

pubpc class CarMapperTest {
   private CarMapper carMapper = Mappers.getMapper(CarMapper.class);
   
   @Test
   pubpc 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: mvnmapping	argetsurefire-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.DepveryAddressMapperTest
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