英文:
Microprofile REST client providers not active if in separate jar file
问题
我想在不同的war文件中使用一些Microprofile REST客户端,使用@RestClient
注入。
我需要为基本身份验证注册一个javax.ws.rs.client.ClientRequestFilter
。如果这个过滤器在我的war项目中,它可以正常工作(例如,使用@RegisterProvider(AuthFilter.class)
这样的org.eclipse.microprofile.rest.client.annotation.RegisterProvider
注解)。但奇怪的是,如果AuthFilter类在外部的jar文件中,它就不再工作了:过滤器不会生效。
我的问题是:如何在我的war项目之外注册一个过滤器?我不想让这个过滤器在使用我的REST API客户端的每个项目中重复出现。
附注:我使用的是wildfly 25.0.1.Final。
英文:
I'd like to use some Microprofile REST client, injected with @RestClient
, in different war files.
I need to register a javax.ws.rs.client.ClientRequestFilter
for basic authentication.
If this filter is in my war project, it works fine (for example with org.eclipse.microprofile.rest.client.annotation.RegisterProvider
like @RegisterProvider(AuthFilter.class)
. Curiously if the AuthFilter class is in an external jar file, it doesn't work any more : the filter is not active.
My question is : how to register a filter outside my war project ? I don't want to have this filter repeated in every project that uses my REST API client.
PS : I use wildfly 25.0.1.Final
答案1
得分: 0
这可能很难在不检查您的整个代码库的情况下确定,但我有一个猜测:您是否在编译后的JAR文件中有一个/META-INF/beans.xml
文件?根据您使用的CDI规范级别,这可能是必需的。
如果您使用Maven构建项目,您应该将此文件放在相对于项目根目录的src/main/resources/META-INF/beans.xml
位置。
以下是一个JEE v8的beans.xml示例:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="annotated"
version="2.0">
</beans>
这里还有所有当前CDI版本的参考链接:https://www.mastertheboss.com/jboss-frameworks/cdi/configuring-beans-xml-file
我另外的猜测是,由于这是一个过滤器类,您可能还需要在同一文件夹中添加一个Web片段文件。
以下是适用于相同JEE v8规范级别的web-fragment.xml示例:
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-fragment_4_0.xsd"
version="4.0">
</web-fragment>
这里还有其他版本的指南链接:https://jakarta.ee/xml/ns/jakartaee/
英文:
It's hard to tell without examining your entire codebase, but I do have a guess: Do you have a /META-INF/beans.xml
file in the compiled jar? Depending on the CDI specification level you are using, that is probably required.
If you're building using maven, you should put this file in src/main/resources/META-INF/beans.xml
relative to the project root.
Here is an example of a beans.xml JEE v8:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="annotated"
version="2.0">
</beans>
And here is a reference for all current CDI versions: https://www.mastertheboss.com/jboss-frameworks/cdi/configuring-beans-xml-file
The other guess I have is that since this is a filter class, you probably also need a web fragment file in the same folder.
Here is an example web-fragment.xml for the same JEE v8 specification level:
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-fragment_4_0.xsd"
version="4.0">
</web-fragment>
And here is a guide to other versions: https://jakarta.ee/xml/ns/jakartaee/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论