You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Quarkus Maven插件提示的MapStruct生成类私有成员不推荐使用警告?

Absolutely! You can configure MapStruct to generate package-private injected fields instead of private ones, which will resolve that Quarkus warning. Here's how to do it:

The Fix: Configure Field Visibility in MapStruct

MapStruct lets you control the visibility of generated fields via the fieldVisibility parameter on the @Mapper annotation (or a shared @MapperConfig for multiple mappers). For your use case, you'll want to set it to AccessLevel.PACKAGE_PRIVATE.

Modified Mapper Interface

Update your SampleMapper to include the visibility setting:

import org.mapstruct.AccessLevel;
import org.mapstruct.Mapper;

@Mapper(
    componentModel = "cdi", 
    uses = SampleFactory.class,
    fieldVisibility = AccessLevel.PACKAGE_PRIVATE
)
public interface SampleMapper {
    // 映射函数
}

Generated Implementation Result

With this change, MapStruct will generate the SampleMapperImpl with a package-private sampleFactory field, matching Quarkus's recommendation:

@ApplicationScoped
public class SampleMapperImpl implements SampleMapper {
    SampleFactory sampleFactory; // No private modifier - package-private
    // 映射函数
}

Bonus: Centralized Configuration for Multiple Mappers

If you have multiple MapStruct mappers in your project, you can avoid repeating the setting by creating a shared @MapperConfig class:

import org.mapstruct.AccessLevel;
import org.mapstruct.MapperConfig;

@MapperConfig(
    componentModel = "cdi",
    fieldVisibility = AccessLevel.PACKAGE_PRIVATE
)
public interface SharedMapperConfig {}

Then reference this config in your individual mappers:

@Mapper(uses = SampleFactory.class, config = SharedMapperConfig.class)
public interface SampleMapper {
    // 映射函数
}

This feature is supported in MapStruct 1.3 and later, so your 1.4.2.Final version works perfectly with it. The Quarkus warning exists because its CDI proxying mechanism operates more efficiently with package-private members (no need to generate accessor methods for private fields), so this fix aligns with both tools' best practices.

内容的提问来源于stack exchange,提问作者Jaqueline Schweigert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:44:07