如何在夸库斯中为外部模块中的类创建 Jandex 索引

首先,我有一个多模块的 maven 层次结构,如下所示:


├── project (parent pom.xml)

│   ├── service

│   ├── api-library

所以现在来看看问题:


我正在服务模块中编写一个 JAX-RS 端点,该模块使用 API 库中的类。

当我开始夸库时,我收到这样的警告:


13:01:18,784 WARN  [io.qua.dep.ste.ReflectiveHierarchyStep] Unable to properly register the hierarchy of the following classes for reflection as they are not in the Jandex index:

- com.example.Fruit

- com.example.Car

Consider adding them to the index either by creating a Jandex index for your dependency or via quarkus.index-dependency properties.

这两个类和位于模块中。com.example.Fruitcom.example.Carapi-library


因此,我认为我需要将它们添加到应用程序属性中的 Jandex 索引依赖项中。


但是,如何将 Jandex 索引依赖性添加到夸库斯中呢?


潇湘沐
浏览 96回答 3
3回答

哆啦的时光机

Quarkus 会自动为主模块编制索引,但是,当您有其他模块包含序列化为 JSON 的 CDI Bean、实体、对象时,您需要显式索引它们。有几个不同(易于实现)的选项可以做到这一点。使用詹德克斯·马文插件只需将以下内容添加到附加模块 pom.xml:<build>&nbsp; <plugins>&nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; <groupId>org.jboss.jandex</groupId>&nbsp; &nbsp; &nbsp; <artifactId>jandex-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; <version>1.2.3</version>&nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>make-index</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>jandex</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; </plugin>&nbsp; </plugins></build>如果你的依赖项位于项目外部,并且你想要一劳永逸地生成索引,则这是最有益的选择。使用渐变詹德克斯插件如果您使用的是 Gradle,则有一个第三方插件允许生成 Jandex 索引:https://github.com/kordamp/jandex-gradle-plugin 。添加一个空的元 INF/豆.xml如果在附加模块中添加一个空文件,则类也将被索引。META-INF/beans.xmlsrc/main/resources这些类将由夸库斯本身编制索引。索引其他依赖项如果无法修改依赖项(例如,考虑第三方依赖项),仍可以通过向 以下项添加条目来为其编制索引:application.propertiesquarkus.index-dependency.<name>.group-id=quarkus.index-dependency.<name>.artifact-id=quarkus.index-dependency.<name>.classifier=(this one is optional)作为您选择的名称来标识您的依赖关系。<name>

波斯汪

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

牧羊人nacy

对于分级用户,您可以在所依赖模块的 build.gradle 中使用此插件。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java