1 回答

TA貢獻1906條經驗 獲得超10個贊
當前的解決方法是將實體 bean 映射到 Java 流或反應流中的 DTO bean,并手動或通過 Mapstruct 進行屬性映射。
更新:這是評論中問題的答案,并舉例說明了如何使用 Mapstruct 進行解決方法:
將 Mapstruct 依賴項添加到build.gradle中:
implementation "org.mapstruct:mapstruct:$mapstructVersion"
annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
testAnnotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
定義映射器:
import org.mapstruct.Mapper;
@Mapper(
? ? componentModel = "jsr330"
)
public interface ParseErrorMapper {
? ? ParseErrorDto entityToDto(@NotNull ParseError parseError);
? ? EntityReference recipeToDto(@NotNull Recipe recipe);
}
這是控制器中該映射器的用法:
@Controller("/parse-error")
public class ParseErrorController {
? ? private final ParseErrorRepository repository;
? ? private final ParseErrorMapper mapper;
? ? public ParseErrorController(ParseErrorRepository repository, ParseErrorMapper mapper) {
? ? ? ? this.repository = repository;
? ? ? ? this.mapper = mapper;
? ? }
? ? @Get("all")
? ? @Transactional
? ? public Page<ParseErrorDto> getAll(final Pageable pageable) {
? ? ? ? return repository.findAll(pageable).map(mapper::entityToDto);
? ? }
}
添加回答
舉報