如何使用modelMapper转换嵌套类(不等于类)
How to use modelMapper to convert nested classes (Not equal classes)
我是后端方面的新手,我想在我的项目中使用 modelmapper。我知道如何在基础级别上使用 modelmapper。
有我的实体classes:
public class Content {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, unique = true)
private int id;
@ManyToOne
@JsonIgnore
@JoinColumn(name = "userId",
referencedColumnName = "userId",
foreignKey = @ForeignKey(name = "fk_content_userId"))
private User user;
@Column(name = "title")
private String title;
@Column(name = "createdDate")
private Double createdDate;
}
public class User {
@Id
@Column(name = "userId", nullable = false, unique = true)
private String userId;
@Column(name = "name")
private String name;
@Column(name = "phoneNumber")
private String phoneNumber;
@Column(name = "email")
private String email;
}
还有我的回复class:
public class Response {
private String title;
private Double createdDate;
private String userId;
}
那我想要什么?
有API的尸体:
response: {
"title": "title",
"createdDate": "1616758604",
"userId": "101010"
}
我想将此回复转换为以下内容:
Content: {
"title": "title",
"createdDate": "1616758604",
"User" : {
"userId" : "101010"
"name" : "",
"phoneNumber" : "",
"email" : ""
}
}
注意:我最后用JSON格式写了“内容”。但我需要它 JAVA.
如果我对你的问题理解正确,你的问题是将 userId 转换为相应的用户并希望使用 ModelMapper Library. Therefore you can use the provided converter class。我使用了您的示例,添加了 getters/setters 并创建了一个将 Response 转换为 Content 对象的 ModelMapper 实例(见下文)。
// First some sample data
User user = new User();
user.userId = "abc";
user.name = "name";
user.email = "mail";
var response = new Response();
response.createdDate = 112390582.;
response.title = "titel";
response.userId = "abc";
// Mock user db
Map<String, User> users = Map.of(user.userId, user);
// The ModelMapper
var mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
Converter<String, User> conv = (in) -> in.getSource() == null ? null : users.get(in.getSource());
mapper.createTypeMap(Response.class, Content.class)
.addMappings(mapping -> mapping.using(conv).map(Response::getUserId, Content::setUser));
// Mapping
Content content = mapper.map(response, Content.class);
我是后端方面的新手,我想在我的项目中使用 modelmapper。我知道如何在基础级别上使用 modelmapper。
有我的实体classes:
public class Content {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, unique = true)
private int id;
@ManyToOne
@JsonIgnore
@JoinColumn(name = "userId",
referencedColumnName = "userId",
foreignKey = @ForeignKey(name = "fk_content_userId"))
private User user;
@Column(name = "title")
private String title;
@Column(name = "createdDate")
private Double createdDate;
}
public class User {
@Id
@Column(name = "userId", nullable = false, unique = true)
private String userId;
@Column(name = "name")
private String name;
@Column(name = "phoneNumber")
private String phoneNumber;
@Column(name = "email")
private String email;
}
还有我的回复class:
public class Response {
private String title;
private Double createdDate;
private String userId;
}
那我想要什么?
有API的尸体:
response: {
"title": "title",
"createdDate": "1616758604",
"userId": "101010"
}
我想将此回复转换为以下内容:
Content: {
"title": "title",
"createdDate": "1616758604",
"User" : {
"userId" : "101010"
"name" : "",
"phoneNumber" : "",
"email" : ""
}
}
注意:我最后用JSON格式写了“内容”。但我需要它 JAVA.
如果我对你的问题理解正确,你的问题是将 userId 转换为相应的用户并希望使用 ModelMapper Library. Therefore you can use the provided converter class。我使用了您的示例,添加了 getters/setters 并创建了一个将 Response 转换为 Content 对象的 ModelMapper 实例(见下文)。
// First some sample data
User user = new User();
user.userId = "abc";
user.name = "name";
user.email = "mail";
var response = new Response();
response.createdDate = 112390582.;
response.title = "titel";
response.userId = "abc";
// Mock user db
Map<String, User> users = Map.of(user.userId, user);
// The ModelMapper
var mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
Converter<String, User> conv = (in) -> in.getSource() == null ? null : users.get(in.getSource());
mapper.createTypeMap(Response.class, Content.class)
.addMappings(mapping -> mapping.using(conv).map(Response::getUserId, Content::setUser));
// Mapping
Content content = mapper.map(response, Content.class);