通过字段中的设置进行补丁映射对象更新的最佳方法
在这样的对象上进行补丁映射的最佳方法是什么:
@Data
public class UnifiedOfferEntity {
private String companyName;
private String city;
private String title;
private Set<SkillEntity> skills;
}
在实体中,我有很多经营。我做了更新的方法,以发送映射&lt; fieldName,value&gt;它可以正常工作,直到我试图将其设置为请求主体为止。
public UnifiedOfferEntity patchEntity(String id, Map<Object, Object> fields) {
UnifiedOfferEntity unifiedOfferEntity = getEntityById(id);
fields.forEach((key, value) -> {
Field field = ReflectionUtils.findField(UnifiedOfferEntity.class, (String) key);
field.setAccessible(true);
ReflectionUtils.setField(field, unifiedOfferEntity, value);
});
return unifiedOfferEntity;
}
我得到:
java.lang.IllegalArgumentException: Can not set java.util.Set field com.example.jobfinder.entity.UnifiedOfferEntity.seniority to java.util.ArrayList
What is the best way to do patch mapping on object like this:
@Data
public class UnifiedOfferEntity {
private String companyName;
private String city;
private String title;
private Set<SkillEntity> skills;
}
In entity I have many to many realtion. I did method for update where I send map<fieldName, value> and it works properly until I am trying to put set to the request body.
public UnifiedOfferEntity patchEntity(String id, Map<Object, Object> fields) {
UnifiedOfferEntity unifiedOfferEntity = getEntityById(id);
fields.forEach((key, value) -> {
Field field = ReflectionUtils.findField(UnifiedOfferEntity.class, (String) key);
field.setAccessible(true);
ReflectionUtils.setField(field, unifiedOfferEntity, value);
});
return unifiedOfferEntity;
}
I am getting:
java.lang.IllegalArgumentException: Can not set java.util.Set field com.example.jobfinder.entity.UnifiedOfferEntity.seniority to java.util.ArrayList
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在这种方法中考虑过:
该方法将根据键作为常数手动填充OBJET ..或另一个对象...
have you thought in this approach:
where the method would manually populate the objet based on keys as constants.. or another object...