Websocket + 漂亮的脸 = AbstractMethodError

Websocket + Pretty Faces = AbstractMethodError

我正在 tomcat 8.5 中构建一个简单的 JSF 2.3 应用程序。我让 websocket 工作,然后我添加了 Pretty Faces。

如果我单独使用它们,它们都可以工作。我的意思是,如果我从 pom 中删除 Pretty Faces 的依赖项,我的 websocket 就会工作。如果我保留依赖关系,并从我的视图中删除 <f:websocket>,Pretty Faces 工作(重定向正常)!

但是,如果我尝试使用 Pretty Faces,当我在视图中有一个 <f:websocket> 时,当我导航到它时会出现 AbstractMethodError。从字面上看,如果我评论标签,其他一切都有效。

我使用 mojarra。在我的 pom 中,我将 https://github.com/javaserverfaces/mojarra/blob/master/README.md, and the default ones 中的依赖项用于 Pretty Faces。

我猜问题要么是我在 this BalusC answer 中读到的依赖项存在冲突,要么是 Pretty Faces 和 Websocket 以某种方式发生冲突。

如有任何帮助,我们将不胜感激。提前致谢。

这里是依赖关系

<!-- Java EE containers -->
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>

<!-- Servlet Containers -->
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.faces</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-shaded</artifactId>
    <version>3.0.0.Final</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency> <!-- Optional, only when <f:websocket> is used. -->
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1</version>
</dependency>

<!-- Pretty Faces -->
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-servlet</artifactId>
    <version>3.4.2.Final</version>
</dependency>
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-config-prettyfaces</artifactId>
    <version>3.4.2.Final</version>
</dependency>

这里例外

Nov 06, 2019 1:49:39 AM com.sun.faces.context.ExceptionHandlerImpl log
FATAL: JSF1073: java.lang.AbstractMethodError caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=null
Nov 06, 2019 1:49:39 AM com.sun.faces.context.ExceptionHandlerImpl log
FATAL: No associated message
java.lang.AbstractMethodError
    at javax.faces.application.ViewHandlerWrapper.getWebsocketURL(ViewHandlerWrapper.java:328)
    at javax.faces.application.ViewHandlerWrapper.getWebsocketURL(ViewHandlerWrapper.java:328)
    at com.sun.faces.push.WebsocketChannelManager.register(WebsocketChannelManager.java:151)
    at com.sun.faces.push.WebsocketChannelManager.register(WebsocketChannelManager.java:142)
    at com.sun.faces.push.WebsocketChannelManager$Proxy$_$$_WeldClientProxy.register(Unknown Source)
    at com.sun.faces.renderkit.html_basic.WebsocketRenderer.encodeEnd(WebsocketRenderer.java:115)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:949)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1912)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:918)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1905)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1908)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1908)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:491)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:194)
    at org.ocpsoft.rewrite.faces.RewriteViewHandler.renderView(RewriteViewHandler.java:196)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:151)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:151)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:126)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:223)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:226)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:470)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:395)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316)
    at org.ocpsoft.rewrite.servlet.impl.HttpRewriteResultHandler.handleResult(HttpRewriteResultHandler.java:42)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.rewrite(RewriteFilter.java:297)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:198)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

更新:

根据 Lincoln 的评论,新版本 PrettyFaces 3.4.3 supportes JSF 2.3. Beside other changes it now makes the RewriteViewHandler extend ViewHandlerWrapper 按照 BalusC 的建议。


背景:

PrettyFaces 3.4.2 似乎与 JSF 2.3 不完全兼容。

非抽象方法 class org.ocpsoft.rewrite.faces.RewriteViewHandler.RewriteViewHandler 扩展 javax.faces.application.ViewHandler 但不覆盖抽象方法 getWebsocketURL.

您可以在 class 路径上使用漂亮的面孔和 JSF 2.3 API 轻松重现此内容:

package my.pkg;

import org.ocpsoft.rewrite.faces.RewriteViewHandler;

public class PrettyFacesTest {
    public static void main(String[] args) {
        new RewriteViewHandler(null).getWebsocketURL(null, null);
    }
}

结果:

Exception in thread "main" java.lang.AbstractMethodError: org.ocpsoft.rewrite.faces.RewriteViewHandler.getWebsocketURL(Ljavax/faces/context/FacesContext;Ljava/lang/String;)Ljava/lang/String;
    at my.pkg.PrettyFacesTest.main(PrettyFacesTest.java:7)

RewriteViewhandler注册在rewrite-integration-faces-3.4.2.Final.jar/META-INF/faces-config.xml:

<application>
    <navigation-handler>org.ocpsoft.rewrite.faces.RewriteNavigationHandler</navigation-handler>
    <view-handler>org.ocpsoft.rewrite.faces.RewriteViewHandler</view-handler>
</application>

我不确定是否有机会阻止此注册。如果是这样,您可以创建一个 CustomRewriteViewHandler 来扩展 RewriteViewHandler,覆盖 getWebsocketURL 并委托调用 super.parent:

import javax.faces.application.ViewHandler;
import javax.faces.context.FacesContext;

import org.ocpsoft.rewrite.faces.RewriteViewHandler;

public class CustomRewriteViewHandler extends RewriteViewHandler {
    public CustomRewriteViewHandler(ViewHandler viewHandler) {
        super(viewHandler);
    }

    @Override
    public String getWebsocketURL(FacesContext context, String channel) {
        return super.parent.getWebsocketURL(context, channel);
    }
}

然后您将注册此 CustomRewriteViewHandler 而不是原来的:

<application>
    <view-handler>my.pkg.CustomRewriteViewHandler</view-handler>
</application>

正如 BalusC 所描述的那样,here 您不能从包含的 jar 中屏蔽 部分 faces-config.xml。 相反,您可以设置 <faces-config ... metadata-complete="true"> ,这会导致 none faces-config.xml 或正在处理的任何包含的 jar 中的注释,因此以您需要重新定义所有 3d 为代价你自己需要的派对用品 faces.config.xml.

正如@Selaron 检测到的,问题是 PrettyFaces 不支持 JSF 2.3 的 Websocket,因为 class RewriteViewHandler 没有实现方法 getWebsocketURL 问题。

他一指出,我就分叉了 PrettyFaces (ocpsoft/rewrite),发现根本不支持 JSF 2.3。所以我尝试升级到 JSF 2.3,这似乎比@Selaron 的回答更干净(至少对我而言)。

Here's the commit 的东西工作。我不会详细说明我必须更改的内容(在提交中进行了解释)。

最后一件事。正如我在问题中提到的,我在 pom.xml 中用于 PrettyFaces 的唯一两个依赖项是 the default ones.

但是当我在本地环境中构建项目时,我需要添加更多内容。

他们来了

<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-impl-servlet</artifactId>
    <version>3.4.3-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-config-prettyfaces</artifactId>
    <version>3.4.3-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-api-servlet</artifactId>
    <version>3.4.3-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-annotations-impl</artifactId>
    <version>3.4.3-SNAPSHOT</version>
</dependency>

Cloning/downloading 来自我的 ocpsoft/rewrite 分支,在本地安装项目,并将这些依赖项添加到您的 pom.xml 应该允许您使 PrettyFaces + JSF 2.3 的 websocket 工作.

PS - 我现在不愿将任何答案标记为已接受。 (我会尝试@Selaron's 然后再决定)。