@@映射注释未正确映射以将Entity对象转换为DTO对象

问题描述 投票:0回答:1

我正在使用@Mapping将我的实体类映射到DTO类。所有值都被完美映射,除了一个hostPlayerId为null。

我正在点击下面的API,该API返回了Lobby对象,并且我正在使用MapStruct将Entity映射到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课程

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;
    }
}

我的实体类

@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;
}

我不明白我在这里想念什么。

java mapstruct
1个回答
0
投票

也许,DTO中的getter和setter必须命名为getHostPlayerId() / setHostPlayerId(),以便映射器可以检测到它们。

而且,由于参数中有错字,您也没有在设置器中分配任何值:

    public void sethostPlayerId(Long hostPlayerIdPlayerId) {
        this.hostPlayerId = hostPlayerId;
    }

应该是:

    public void sethostPlayerId(Long hostPlayerId) {
        this.hostPlayerId = hostPlayerId;
    }
© www.soinside.com 2019 - 2024. All rights reserved.