@Mapping 注释未正确映射以将 Entity 对象转换为 DTO 对象
@Mapping annotation is not mapping correctly to convert Entity object to DTO object
我正在使用 @Mapping 将我的实体 class 映射到 DTO class.All 值被完美映射,除了一个 hostPlayerId 变为空。
我正在点击下面的 API,它正在返回 Lobby 对象,我正在使用 MapStruct 将实体映射到 DTO。它映射除 hostPlayerId 字段之外的每个字段。
@GetMapping("/lobby/{id}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public LobbyGetDTO getLobbyInfo(@RequestHeader("X-Auth-Token") String token, @PathVariable("id") long id) {
Lobby lobby = lobbyService.getLobby(id);
log.info("here here "+lobby.toString());
LobbyGetDTO lobbyInfoDTO = DTOMapper.INSTANCE.convertEntityToLobbyGetDTO(lobby);
log.info(lobbyInfoDTO.toString());
return lobbyInfoDTO;
}
点击 API -
后的记录器
2020-04-18 00:00:01.738 INFO 18486 --- [nio-8080-exec-6] c.u.i.s.s.controller.LobbyController : here here Lobby{id=2, name='Test Lobby', hostPlayerId=1, playerIds=[], chatId=null, gameId=null, status=0}
2020-04-18 00:00:01.740 INFO 18486 --- [nio-8080-exec-6] c.u.i.s.s.controller.LobbyController : LobbyGetDTO{id=2, name='Test Lobby', hostPlayerId=null, playerIds=[], gameId=null}
地图界面-
@Mapper
public interface DTOMapper {
DTOMapper INSTANCE = Mappers.getMapper(DTOMapper.class);
@Mapping(source = "id", target = "id")
@Mapping(source = "name", target = "name")
@Mapping(source = "gameId", target = "gameId")
@Mapping(source = "hostPlayerId", target = "hostPlayerId")
@Mapping(source = "playerIds", target = "playerIds")
LobbyGetDTO convertEntityToLobbyGetDTO(Lobby lobby);
}
构建阶段创建的映射实现方法-
@Override
public LobbyGetDTO convertEntityToLobbyGetDTO(Lobby lobby) {
if ( lobby == null ) {
return null;
}
LobbyGetDTO lobbyGetDTO = new LobbyGetDTO();
lobbyGetDTO.setGameId( lobby.getGameId() );
lobbyGetDTO.setName( lobby.getName() );
lobbyGetDTO.setId( lobby.getId() );
List<Long> list = lobby.getPlayerIds();
if ( list != null ) {
lobbyGetDTO.setPlayerIds( new ArrayList<Long>( list ) );
}
lobbyGetDTO.sethostPlayerId( lobby.gethostPlayerId() );
return lobbyGetDTO;
}
我的 DTO class
public class LobbyGetDTO {
private Long id;
private String name;
private Long hostPlayerId;
private List<Long> playerIds;
private Long gameId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long gethostPlayerId() {
return hostPlayerId;
}
public void sethostPlayerId(Long hostPlayerIdPlayerId) {
this.hostPlayerId = hostPlayerId;
}
public List<Long> getPlayerIds() {
return playerIds;
}
public void setPlayerIds(List<Long> playerIds) {
this.playerIds = playerIds;
}
public Long getGameId() {
return gameId;
}
public void setGameId(Long gameId) {
this.gameId = gameId;
}
}
我的实体class
@Entity
@Table(name = "LOBBY")
public class Lobby implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Long hostPlayerId;
}
我不明白我在这里错过了什么。
也许,DTO 中的 getter 和 setter 需要命名为 getHostPlayerId() / setHostPlayerId()
以便映射器可以检测到它们。
此外,由于参数中的拼写错误,您没有在 setter 中分配任何值:
public void sethostPlayerId(Long hostPlayerIdPlayerId) {
this.hostPlayerId = hostPlayerId;
}
应该是:
public void setHostPlayerId(Long hostPlayerId) {
this.hostPlayerId = hostPlayerId;
}
我正在使用 @Mapping 将我的实体 class 映射到 DTO class.All 值被完美映射,除了一个 hostPlayerId 变为空。
我正在点击下面的 API,它正在返回 Lobby 对象,我正在使用 MapStruct 将实体映射到 DTO。它映射除 hostPlayerId 字段之外的每个字段。
@GetMapping("/lobby/{id}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public LobbyGetDTO getLobbyInfo(@RequestHeader("X-Auth-Token") String token, @PathVariable("id") long id) {
Lobby lobby = lobbyService.getLobby(id);
log.info("here here "+lobby.toString());
LobbyGetDTO lobbyInfoDTO = DTOMapper.INSTANCE.convertEntityToLobbyGetDTO(lobby);
log.info(lobbyInfoDTO.toString());
return lobbyInfoDTO;
}
点击 API -
后的记录器2020-04-18 00:00:01.738 INFO 18486 --- [nio-8080-exec-6] c.u.i.s.s.controller.LobbyController : here here Lobby{id=2, name='Test Lobby', hostPlayerId=1, playerIds=[], chatId=null, gameId=null, status=0}
2020-04-18 00:00:01.740 INFO 18486 --- [nio-8080-exec-6] c.u.i.s.s.controller.LobbyController : LobbyGetDTO{id=2, name='Test Lobby', hostPlayerId=null, playerIds=[], gameId=null}
地图界面-
@Mapper
public interface DTOMapper {
DTOMapper INSTANCE = Mappers.getMapper(DTOMapper.class);
@Mapping(source = "id", target = "id")
@Mapping(source = "name", target = "name")
@Mapping(source = "gameId", target = "gameId")
@Mapping(source = "hostPlayerId", target = "hostPlayerId")
@Mapping(source = "playerIds", target = "playerIds")
LobbyGetDTO convertEntityToLobbyGetDTO(Lobby lobby);
}
构建阶段创建的映射实现方法-
@Override
public LobbyGetDTO convertEntityToLobbyGetDTO(Lobby lobby) {
if ( lobby == null ) {
return null;
}
LobbyGetDTO lobbyGetDTO = new LobbyGetDTO();
lobbyGetDTO.setGameId( lobby.getGameId() );
lobbyGetDTO.setName( lobby.getName() );
lobbyGetDTO.setId( lobby.getId() );
List<Long> list = lobby.getPlayerIds();
if ( list != null ) {
lobbyGetDTO.setPlayerIds( new ArrayList<Long>( list ) );
}
lobbyGetDTO.sethostPlayerId( lobby.gethostPlayerId() );
return lobbyGetDTO;
}
我的 DTO class
public class LobbyGetDTO {
private Long id;
private String name;
private Long hostPlayerId;
private List<Long> playerIds;
private Long gameId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long gethostPlayerId() {
return hostPlayerId;
}
public void sethostPlayerId(Long hostPlayerIdPlayerId) {
this.hostPlayerId = hostPlayerId;
}
public List<Long> getPlayerIds() {
return playerIds;
}
public void setPlayerIds(List<Long> playerIds) {
this.playerIds = playerIds;
}
public Long getGameId() {
return gameId;
}
public void setGameId(Long gameId) {
this.gameId = gameId;
}
}
我的实体class
@Entity
@Table(name = "LOBBY")
public class Lobby implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Long hostPlayerId;
}
我不明白我在这里错过了什么。
也许,DTO 中的 getter 和 setter 需要命名为 getHostPlayerId() / setHostPlayerId()
以便映射器可以检测到它们。
此外,由于参数中的拼写错误,您没有在 setter 中分配任何值:
public void sethostPlayerId(Long hostPlayerIdPlayerId) {
this.hostPlayerId = hostPlayerId;
}
应该是:
public void setHostPlayerId(Long hostPlayerId) {
this.hostPlayerId = hostPlayerId;
}