Si le travail de remplissage est effectué manuellement pour la conversion des haricots, la quantité de code augmentera et la maintenabilité n'est pas bonne. Il existe des méthodes utiles comme BeanUtils.copyProperties, mais rassemblons d'autres bibliothèques de mappage.
FasterXML ObjectMapper https://github.com/FasterXML/jackson-databind
Ceci est utile pour le traitement lié à JSON.
build.gradle
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8'
package com.test.lombok;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class User {
private String userId;
private String userName;
private String age;
private String gender;
}
user.json
{
"userId": "user001",
"userName": "Tester Taro",
"age": 20,
"gender": "famale"
}
package com.test.lombok;
import java.io.File;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestMain {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
//Spécifié par le chemin du fichier
User user1 = mapper.readValue(
new File("C:\\workspace_sts\\lombok\\src\\main\\java\\com\\test\\lombok\\user.json"), User.class);
System.out.println(user1);
//Spécifiez au format URL
User user2 = mapper.readValue(TestMain.class.getResource("user.json"), User.class);
System.out.println(user2);
//Convertir à partir d'une chaîne
User user3 = mapper.readValue(
"{\"userId\":\"user001\", \"userName\": \"Tester Taro 2\", \"age\": 22, \"gender\": \"male\"}", User.class);
System.out.println(user3);
//Convertir d'objet en chaîne
String jsonString = mapper.writeValueAsString(user3);
System.out.println(jsonString);
//Convertir de chaîne en carte
Map<String, String> userMap = mapper.readValue(jsonString, Map.class);
System.out.println(userMap);
}
}
résultat:
Utilisateur (userId = user001, userName = test Taro, age = 20, gender = famale) Utilisateur (userId = user001, userName = test Taro, age = 20, gender = famale) Utilisateur (userId = user001, userName = test Taro 2, age = 22, gender = male) {"userId": "user001", "userName": "Test Taro 2", "age": "22", "gender": "male"} {userId = user001, userName = test Taro 2, age = 22, gender = male}
ModelMapper http://modelmapper.org/
build.gradle
// https://mvnrepository.com/artifact/org.modelmapper/modelmapper
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.5'
package com.test.lombok;
import lombok.Data;
@Data
public class User {
private String userId;
private String userName;
private String age;
private String gender;
private Address addr;
}
@Data
class Address {
private String zipCode;
private String address1;
private String address2;
private String address3;
}
package com.test.lombok;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class UserDTO {
private String userId;
private String userName;
private String age;
private String gender;
private String addrZipCode;
private String addrAddress1;
private String addrAddress2;
private String addrAddress3;
}
package com.test.lombok;
import org.modelmapper.ModelMapper;
public class TestMain {
public static void main(String[] args) throws Exception {
ModelMapper modelMapper = new ModelMapper();
User user = new User();
user.setUserId("user001");
user.setUserName("tester");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("○○ préfecture");
user.setAddr(addr );
UserDTO userDTO = modelMapper.map(user, UserDTO.class);
System.out.println(userDTO);
}
}
résultat:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = 984-0031, addrAddress1 = ○○ prefecture, addrAddress2 = null, addrAddress3 = null)
package com.test.lombok;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
public class TestMain {
public static void main(String[] args) throws Exception {
ModelMapper modelMapper = new ModelMapper();
User user = new User();
user.setUserId("user001");
user.setUserName("tester");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("○○ préfecture");
user.setAddr(addr);
TypeMap<User, UserDTO> typeMap = modelMapper.typeMap(User.class, UserDTO.class).addMappings(mapper -> {
mapper.map(src -> src.getAddr().getZipCode(), UserDTO::setAddrZipCode);
mapper.map(src -> src.getAddr().getAddress1(), UserDTO::setAddrAddress1);
});
System.out.println(typeMap.map(user));
}
}
résultat:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = 984-0031, addrAddress1 = ○○ prefecture, addrAddress2 = null, addrAddress3 = null)
Il est pratique de l'utiliser pour convertir Form Bean en votre propre DTO avec SpringBoot Controller.
MapStruct https://mapstruct.org/
build.gradle
plugins {
id 'java'
id 'net.ltgt.apt' version '0.20'
}
repositories {
jcenter()
}
dependencies {
// lombok
compileOnly "org.projectlombok:lombok:1.18.12"
annotationProcessor "org.projectlombok:lombok:1.18.12"
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct
implementation group: 'org.mapstruct', name: 'mapstruct', version: '1.3.1.Final'
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.3.1.Final'
}
[compileJava, compileTestJava]*.options*.encoding= "UTF-8"
sourceSets {
main.java.srcDirs += "build/generated/sources/annotationProcessor/java/main"
}
package com.test.lombok;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
@Mapping(source = "addr.zipCode", target = "addrZipCode")
@Mapping(source = "addr.address1", target = "addrAddress1")
@Mapping(source = "addr.address2", target = "addrAddress2")
UserDTO userToUserDTO(User user);
}
Après avoir défini et construit ci-dessus, la classe UserMapperImpl.java sera générée dans build \ generated \ sources \ annotationProcessor \ java \ main \ com \ test \ lombok.
UserMapperImpl.java
package com.test.lombok;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-06-28T21:47:47+0900",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_201 (Oracle Corporation)"
)
public class UserMapperImpl implements UserMapper {
@Override
public UserDTO userToUserDTO(User user) {
if ( user == null ) {
return null;
}
UserDTO userDTO = new UserDTO();
userDTO.setAddrAddress2( userAddrAddress2( user ) );
userDTO.setAddrAddress1( userAddrAddress1( user ) );
userDTO.setAddrZipCode( userAddrZipCode( user ) );
userDTO.setUserId( user.getUserId() );
userDTO.setUserName( user.getUserName() );
userDTO.setAge( user.getAge() );
userDTO.setGender( user.getGender() );
return userDTO;
}
private String userAddrAddress2(User user) {
if ( user == null ) {
return null;
}
Address addr = user.getAddr();
if ( addr == null ) {
return null;
}
String address2 = addr.getAddress2();
if ( address2 == null ) {
return null;
}
return address2;
}
private String userAddrAddress1(User user) {
if ( user == null ) {
return null;
}
Address addr = user.getAddr();
if ( addr == null ) {
return null;
}
String address1 = addr.getAddress1();
if ( address1 == null ) {
return null;
}
return address1;
}
private String userAddrZipCode(User user) {
if ( user == null ) {
return null;
}
Address addr = user.getAddr();
if ( addr == null ) {
return null;
}
String zipCode = addr.getZipCode();
if ( zipCode == null ) {
return null;
}
return zipCode;
}
}
package com.test.lombok;
public class TestMain {
public static void main(String[] args) throws Exception {
User user = new User();
user.setUserId("user001");
user.setUserName("tester");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("○○ préfecture");
user.setAddr(addr);
UserDTO userDTO = UserMapper.INSTANCE.userToUserDTO(user);
System.out.println(userDTO);
}
}
résultat:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = 984-0031, addrAddress1 = ○○ prefecture, addrAddress2 = null, addrAddress3 = null)
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.test.lombok.TestMain.main(TestMain.java:18)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.test.lombok.UserMapper
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61)
at com.test.lombok.UserMapper.<clinit>(UserMapper.java:9)
... 1 more
Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.test.lombok.UserMapper
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75)
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58)
Ajout de sourcesSets à build.gradle
build.gradle
sourceSets {
main.java.srcDirs += "build/generated/sources/annotationProcessor/java/main"
}
Détails Doc: https://mapstruct.org/documentation/stable/reference/html/
Dozer https://github.com/DozerMapper/dozer
build.gradle
// https://mvnrepository.com/artifact/com.github.dozermapper/dozer-core
compile group: 'com.github.dozermapper', name: 'dozer-core', version: '6.5.0'
package com.test.lombok;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
public class TestMain {
public static void main(String[] args) throws Exception {
User user = new User();
user.setUserId("user001");
user.setUserName("tester");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("○○ préfecture");
user.setAddr(addr);
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
UserDTO userDTO = mapper.map(user, UserDTO.class);
System.out.println(userDTO);
}
}
résultat:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = null, addrAddress1 = null, addrAddress2 = null, addrAddress3 = null)
Le même nom d'élément a été copié, mais sinon, il était nul.
Si le nom de l'élément est différent, il peut être défini comme @Mapping (" binaryData ")
, mais s'il s'agit d'un type compliqué, il doit être défini en XML.
Je pense que vous pouvez choisir votre bibliothèque préférée.
c'est tout
Recommended Posts