为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

潇湘沐 2022-09-21 16:58:00
首先,我有一个多模块的 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.CarConsider 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 索引依赖性添加到夸库斯中呢?
查看完整描述

3 回答

?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

Quarkus 会自动为主模块编制索引,但是,当您有其他模块包含序列化为 JSON 的 CDI Bean、实体、对象时,您需要显式索引它们。


有几个不同(易于实现)的选项可以做到这一点。


使用詹德克斯·马文插件


只需将以下内容添加到附加模块 pom.xml:


<build>

  <plugins>

    <plugin>

      <groupId>org.jboss.jandex</groupId>

      <artifactId>jandex-maven-plugin</artifactId>

      <version>1.2.3</version>

      <executions>

        <execution>

          <id>make-index</id>

          <goals>

            <goal>jandex</goal>

          </goals>

        </execution>

      </executions>

    </plugin>

  </plugins>

</build>

如果你的依赖项位于项目外部,并且你想要一劳永逸地生成索引,则这是最有益的选择。


使用渐变詹德克斯插件


如果您使用的是 Gradle,则有一个第三方插件允许生成 Jandex 索引:https://github.com/kordamp/jandex-gradle-plugin 。


添加一个空的元 INF/豆.xml


如果在附加模块中添加一个空文件,则类也将被索引。META-INF/beans.xmlsrc/main/resources


这些类将由夸库斯本身编制索引。


索引其他依赖项


如果无法修改依赖项(例如,考虑第三方依赖项),仍可以通过向 以下项添加条目来为其编制索引:application.properties


quarkus.index-dependency.<name>.group-id=

quarkus.index-dependency.<name>.artifact-id=

quarkus.index-dependency.<name>.classifier=(this one is optional)

作为您选择的名称来标识您的依赖关系。<name>


查看完整回答
反对 回复 2022-09-21
?
波斯汪

TA贡献1811条经验 获得超4个赞

现在,在我的微服务中,我广泛使用注释中的属性。这是根据文档的属性说明:targetsRegisterForReflection


/**

 * 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.

 */

这在基于夸库的项目上工作得很好,并且当您想要注册几个POJO进行反射时,可以处理基本情况。注释将自行注册 POJO,但不会从 POJO 的方法注册返回类型。RegisterForReflection


更高级的方法是使用此处所述的注释。我正在将其与反射库和定制的实用程序包装器一起使用:反射实用程序@AutomaticFeature


现在我可以做更复杂的任务了:


@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 索引,添加 bean.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();

}


查看完整回答
反对 回复 2022-09-21
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

对于分级用户,您可以在所依赖模块的 build.gradle 中使用此插件。


查看完整回答
反对 回复 2022-09-21
  • 3 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信