我有一个用例,我想将对象映射到 ByteBuffer 进行传输......
@Mapper
public static interface ByteBufferMapper {
public static ByteBufferMapper INSTANCE = Mappers.getMapper(ByteBufferMapper.class);
default byte toByte(ByteBuffer buffer) {
byte b = buffer.get();
return b;
}
}
public static class Dto {
public byte b;
public byte bb;
...
}
@Mapper(uses = ByteBufferMapper.class)
public static interface DtoMapper {
public static DtoMapper INSTANCE = Mappers.getMapper(DtoMapper.class);
@Mapping(source = "buffer", target = "bb")
@Mapping(source = "buffer", target = "b")
Dto byteBufferToDto(ByteBuffer buffer);
}
public static void main( String[] args ) {
ByteBuffer buffer = ByteBuffer.allocate(2).put((byte) 0xFF).put((byte) 0x00).flip();
System.out.println(DtoMapper.INSTANCE.byteBufferToDto(buffer));
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以控制 MapStructs 映射顺序,以便b变量填充 0xFF 并bb填充 0x00 值?
是的,您可以通过@Mapping.dependsOn.
像这样:
@Mappings({
@Mapping(target = "surName", source = "lastName", dependsOn = "middleName"),
@Mapping(target = "middleName", dependsOn = "givenName"),
@Mapping(target = "givenName", source = "firstName")
})
AddressDto addressToDto(Address address);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7796 次 |
| 最近记录: |