地图列表<对象>列表< objectsrv>
我想映射列表<城市>列表< Citysrv>在服务层中,但我不知道该怎么做。
我想映射的City SRV:(SRV =服务器响应值)
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class CitySrv {
private Long id;
private String name;
private Long state;
}
想要映射的城市实体:
@AllArgsConstructor
@NoArgsConstructor
@Entity
@SuperBuilder
@Getter
@Setter
@Table(name = "IOTP_CITY")
public class City extends BaseEntity {
@Column(name = "NAME")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "state_id")
private State state;
@OneToMany(mappedBy = "city")
private List<Branch> branches = new ArrayList<Branch>();
}
我以前使用过Model Mapper,但由于某些原因,我无法再使用它。
@Override
public GenericResponse<List<CitySrv>> getAllCities() throws ProjectException {
Optional<List<City>> citiiesSrvResponse = cityRepository.getAllCities();
if (citiiesSrvResponse.isPresent()) {
List<CitySrv> citiesList = modelMapper.map(citiiesSrvResponse.get(), new
TypeToken<List<CitySrv>>() {
}.getType());
return ResponseUtil.getResponse(citiesList);
} else throw new ProjectException(ExceptionStatus.NOT_FOUND);
}
I want to Map List < City > to List < CitySrv > in the service layer but i dont have any idea how to do that.
the City srv that i want to Map: (srv = server response value)
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class CitySrv {
private Long id;
private String name;
private Long state;
}
the City Entity that want to map From:
@AllArgsConstructor
@NoArgsConstructor
@Entity
@SuperBuilder
@Getter
@Setter
@Table(name = "IOTP_CITY")
public class City extends BaseEntity {
@Column(name = "NAME")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "state_id")
private State state;
@OneToMany(mappedBy = "city")
private List<Branch> branches = new ArrayList<Branch>();
}
i used Model mapper before but for some reason i cant use it anymore.
@Override
public GenericResponse<List<CitySrv>> getAllCities() throws ProjectException {
Optional<List<City>> citiiesSrvResponse = cityRepository.getAllCities();
if (citiiesSrvResponse.isPresent()) {
List<CitySrv> citiesList = modelMapper.map(citiiesSrvResponse.get(), new
TypeToken<List<CitySrv>>() {
}.getType());
return ResponseUtil.getResponse(citiesList);
} else throw new ProjectException(ExceptionStatus.NOT_FOUND);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我像这样处理它,但不是很好
i handle it just like this but its not so good
为了使模型设备工作要工作,您的来源和目的地必须足够相似。您想将城市中的实体状态映射到CitySRV的ID。我猜想无法满足这一标准。您必须告诉ModelMapper如何通过配置
最佳映射您在全局配置中进行映射。
您可以在此
For modelMapper to work your source and destination have to be similar enough. You want to map the entity State in City to a Long which is its id in CitySrv. Which I would guess doesn't fulfill this criteria. You have to tell modelMapper how to map that by configuring a mapping
Best you do that in a global config.
You can find more info in this baeldung article