3 回答

TA貢獻1779條經(jīng)驗 獲得超6個贊
Quarkus 會自動為主模塊編制索引,但是,當您有其他模塊包含序列化為 JSON 的 CDI Bean、實體、對象時,您需要顯式索引它們。
有幾個不同(易于實現(xiàn))的選項可以做到這一點。
使用詹德克斯·馬文插件
只需將以下內(nèi)容添加到附加模塊 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>

TA貢獻1811條經(jīng)驗 獲得超4個贊
現(xiàn)在,在我的微服務中,我廣泛使用注釋中的屬性。這是根據(jù)文檔的屬性說明: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
現(xiàn)在我可以做更復雜的任務了:
@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) 未注冊為在本機模式下進行反射。因此,我最終獲得了快速而骯臟的解決方案來注冊它(見下文)。我創(chuàng)建了一個未使用的 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();
}
添加回答
舉報