首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Quarkus中为外部模块中的类创建Jandex索引

如何在Quarkus中为外部模块中的类创建Jandex索引
EN

Stack Overflow用户
提问于 2019-04-04 10:20:37
回答 3查看 18.6K关注 0票数 39

首先,我有一个像这样的多模块maven层次结构:

代码语言:javascript
复制
├── project (parent pom.xml)
│   ├── service
│   ├── api-library

所以现在我们来谈谈问题:

我正在服务模块中编写JAX端点,它使用api库中的类。

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

代码语言:javascript
复制
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.Car位于api-library模块中。

因此,我认为我需要将它们添加到application.properties中的Jandex索引依赖项中。

,但我如何将Jandex索引依赖项添加到夸克中呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-04 10:31:56

Quarkus自动对主模块进行索引,但是,当您有包含CDI、实体、序列化为JSON的对象的附加模块时,您需要显式地对它们进行索引。

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

使用Jandex插件的

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

代码语言:javascript
复制
<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插件

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

添加一个空的META/beans.xml

如果在附加模块META-INF/beans.xml中添加了一个空的src/main/resources文件,那么这些类也将被编入索引。

这些类将由Quarkus本身索引。

索引其他依赖项

如果不能修改依赖项(例如,考虑第三方依赖项),则仍然可以通过向application.properties添加一个条目来对其进行索引。

代码语言:javascript
复制
quarkus.index-dependency.<name>.group-id=
quarkus.index-dependency.<name>.artifact-id=
quarkus.index-dependency.<name>.classifier=(this one is optional)

如果<name>是一个名称,则可以选择标识您的依赖项。

票数 52
EN

Stack Overflow用户

发布于 2019-08-07 17:21:58

编辑(11/02/2020)

现在,在我的微服务中,我广泛地使用了来自targets注释的RegisterForReflection属性。这是根据文件所作的财产解释:

代码语言:javascript
复制
/**
 * 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以进行反射时,它可以处理基本情况。RegisterForReflection注释将自己注册POJO,但不会注册POJO方法中的返回类型。

更高级的方法是使用@AutomaticFeature注释,如描述的这里。我将它与反射库和定制的实用程序包装器:ReflectUtils一起使用。

现在我可以完成更复杂的任务:

代码语言:javascript
复制
@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端点,它返回类。

代码语言:javascript
复制
/**
 * 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();
}
票数 1
EN

Stack Overflow用户

发布于 2020-01-17 04:17:24

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55513502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档