编辑(2020 年 11 月 2 日)
现在在我的微服务中,我广泛使用注释中的targets
属性。RegisterForReflection
这是根据文档的属性解释:
/**
* Alternative classes that should actually be registered for reflection instead of the current class.
*
* This allows for classes in 3rd party libraries to be registered without modification or writing an
* extension. If this is set then the class it is placed on is not registered for reflection, so this should
* generally just be placed on an empty class that is not otherwise used.
*/
这在基于 quarkus 的项目上运行良好,并且可以在您想要注册少量 POJO 以进行反射时处理基本情况。注解会自己注册 POJO ,RegisterForReflection
但不会注册 POJO 方法的返回类型。
更高级的方法是使用此处@AutomaticFeature
描述的注释。我将它与反射库和定制的实用程序包装器一起使用:ReflectUtils
现在我可以完成更复杂的任务:
@AutomaticFeature
@RegisterForReflection(targets = {
com.hotelbeds.hotelapimodel.auto.convert.json.DateSerializer.class,
TimeDeserializer.class,
DateSerializer.class,
TimeSerializer.class,
RateSerializer.class,
})
public class HotelBedsReflection implements Feature {
public static Logger log = Utils.findLogger(Reflections.class);
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
ReflectUtils.registerPackage(LanguagesRQ.class.getPackage().getName(), Object.class);
ReflectUtils.registerPackage(AvailabilityRQ.class.getPackage().getName(), Object.class);
ReflectUtils.registerPackage(Occupancy.class.getPackage().getName(), Object.class);
}
}
初步答案
我尝试添加 Jandex 索引,添加 beans.xml 以及索引其他依赖项,如@emre-işık 答案中所述,但是我的第三方类(EpAutomationRs)未注册以在本机模式下进行反射。所以我最终找到了快速而肮脏的注册解决方案(见下文)。我创建了一个返回类的未使用的 REST JSON 端点。
/**
* the purpose of this method is to register for reflection EpAutomationRs class
*
* @return
*/
@GET
@Path(GET_EMPTY_RS)
@Produces(MediaType.APPLICATION_JSON)
public EpAutomationRs entry() {
return new EpAutomationRs();
}