Wenn die Nachfüllarbeiten für die Bean-Konvertierung manuell ausgeführt werden, erhöht sich die Codemenge und die Wartbarkeit ist nicht gut. Es gibt nützliche Methoden wie BeanUtils.copyProperties, aber lassen Sie uns andere Mapping-Bibliotheken zusammenstellen.
FasterXML ObjectMapper https://github.com/FasterXML/jackson-databind
Dies ist nützlich für die JSON-bezogene Verarbeitung.
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": "Testen Sie 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();
//Wird durch den Dateipfad angegeben
User user1 = mapper.readValue(
new File("C:\\workspace_sts\\lombok\\src\\main\\java\\com\\test\\lombok\\user.json"), User.class);
System.out.println(user1);
//Geben Sie im URL-Format an
User user2 = mapper.readValue(TestMain.class.getResource("user.json"), User.class);
System.out.println(user2);
//Von Zeichenfolge konvertieren
User user3 = mapper.readValue(
"{\"userId\":\"user001\", \"userName\": \"Testen Sie Taro 2\", \"age\": 22, \"gender\": \"male\"}", User.class);
System.out.println(user3);
//Vom Objekt zum String konvertieren
String jsonString = mapper.writeValueAsString(user3);
System.out.println(jsonString);
//Von Zeichenfolge in Karte konvertieren
Map<String, String> userMap = mapper.readValue(jsonString, Map.class);
System.out.println(userMap);
}
}
Ergebnis:
Benutzer (userId = user001, userName = test Taro, Alter = 20, Geschlecht = famale) Benutzer (userId = user001, userName = test Taro, Alter = 20, Geschlecht = famale) Benutzer (userId = user001, userName = test Taro 2, Alter = 22, Geschlecht = männlich) {"userId": "user001", "userName": "Test Taro 2", "age": "22", "gender": "male"} {userId = user001, userName = test Taro 2, Alter = 22, Geschlecht = männlich}
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("Prüfung");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("Präfektur");
user.setAddr(addr );
UserDTO userDTO = modelMapper.map(user, UserDTO.class);
System.out.println(userDTO);
}
}
Ergebnis:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = 984-0031, addrAddress1 = ○○ Präfektur, 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("Prüfung");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("Präfektur");
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));
}
}
Ergebnis:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = 984-0031, addrAddress1 = ○○ Präfektur, addrAddress2 = null, addrAddress3 = null)
Mit SpringBoot Controller können Sie Form Bean bequem in Ihr eigenes DTO konvertieren.
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);
}
Nach dem Definieren und Erstellen oben wird die UserMapperImpl.java-Klasse in build \ generate \ sources \ annotationProcessor \ java \ main \ com \ test \ lombok generiert.
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("Prüfung");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("Präfektur");
user.setAddr(addr);
UserDTO userDTO = UserMapper.INSTANCE.userToUserDTO(user);
System.out.println(userDTO);
}
}
Ergebnis:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = 984-0031, addrAddress1 = ○○ Präfektur, 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)
SourceSets zu build.gradle hinzugefügt
build.gradle
sourceSets {
main.java.srcDirs += "build/generated/sources/annotationProcessor/java/main"
}
Details 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("Prüfung");
user.setAge("18");
user.setGender("male");
Address addr = new Address();
addr.setZipCode("984-0031");
addr.setAddress1("Präfektur");
user.setAddr(addr);
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
UserDTO userDTO = mapper.map(user, UserDTO.class);
System.out.println(userDTO);
}
}
Ergebnis:
UserDTO (userId = user001, userName = test, age = 18, gender = male, addrZipCode = null, addrAddress1 = null, addrAddress2 = null, addrAddress3 = null)
Der gleiche Elementname wurde kopiert, ansonsten war er null.
Wenn der Elementname unterschiedlich ist, kann er wie "@Mapping (" binaryData ")" festgelegt werden. Wenn es sich jedoch um einen komplizierten Typ handelt, muss er in XML festgelegt werden.
Ich denke, Sie können Ihre Lieblingsbibliothek auswählen.
das ist alles
Recommended Posts