[JAVA] How to use ModelMapper (Spring boot)

Memo Memo.

How to use ModelMapper

Creating a new user is the easiest to understand. Map the input user information and user Entity (DTO).

It looks like this in a legacy project using Struts of the project that is currently in. An image to rewrite from the received form information.

When not using modelMapper



@PostMapping("/new")
    public String newUser(@Validated @ModelAttribute("userForm") UserForm form, BindingResult br) {

        User user = User.builder()
                .name(form.getName())
                .email(form.getEmail())
                .birthDay(form.getBirthDay())
                .zip(form.getZip())
                .address(form.getAddress())
                .build();

With modelMapper

With ModelMapper



@PostMapping("/new")
    public String newUser(@Validated @ModelAttribute("userForm") UserForm form, BindingResult br) {

         ModelMapper modelMapper = new ModelMapper();
        //Create a DTO from the input values
        val inputUser = modelMapper.map(form, User.class);

Refreshing! !! To use it, the following description is required in the dependency configuration file such as build.gradle and pom.xml.

build.gradle


dependencies {

    // modelmapper
    compile "org.modelmapper:modelmapper:0.7.5"


}

Recommended Posts

How to use ModelMapper (Spring boot)
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to use Spring Boot session attributes (@SessionAttributes)
How to use Lombok in Spring
How to use Spring Data JDBC
How to set Spring Boot + PostgreSQL
How to use CommandLineRunner in Spring Batch of Spring Boot
How to split Spring Boot message file
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to make Spring Boot Docker Image smaller
How to add a classpath in Spring Boot
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
Spring Boot --How to set session timeout time
[Java] How to use Map
Try Spring Boot from 0 to 100.
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
[Swift] How to use UserDefaults
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
Introduction to Spring Boot ① ~ DI ~
How to use String [] args
[Java] How to use string.format
Introduction to Spring Boot ② ~ AOP ~
How to use rails join
How to use Java Map
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation