java.lang.NoSuchMethodException 尝试使用 OncePerRequestFilter [重复]

Posted

技术标签:

【中文标题】java.lang.NoSuchMethodException 尝试使用 OncePerRequestFilter [重复]【英文标题】:java.lang.NoSuchMethodException when trying to use OncePerRequestFilter [duplicate] 【发布时间】:2021-01-25 21:48:34 【问题描述】:

我正在尝试使用 OncePerRequestFilter 实现过滤传入的 HTTP 请求。用于此的 bean 已成功实例化,但是,当使用 mvn jetty:run 运行项目时,我得到一个带有以下堆栈跟踪的 java.lang.NoSuchMethodException

javax.servlet.ServletException: java.lang.NoSuchMethodException: com.nextscm.proxy.config.InboundLoggingFilter.<init>()
    at org.eclipse.jetty.servlet.ServletContextHandler$Context.createFilter (ServletContextHandler.java:1362)
    at org.eclipse.jetty.servlet.FilterHolder.initialize (FilterHolder.java:122)
    at org.eclipse.jetty.servlet.ServletHandler.initialize (ServletHandler.java:741)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext (ServletContextHandler.java:374)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp (WebAppContext.java:1497)
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp (JettyWebAppContext.java:360)
    at org.eclipse.jetty.webapp.WebAppContext.startContext (WebAppContext.java:1459)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart (ContextHandler.java:847)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart (ServletContextHandler.java:287)
    at org.eclipse.jetty.webapp.WebAppContext.doStart (WebAppContext.java:545)
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart (JettyWebAppContext.java:428)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:138)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:117)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:113)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart (ContextHandlerCollection.java:168)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:138)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:117)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:113)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:138)
    at org.eclipse.jetty.server.Server.start (Server.java:416)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:108)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:113)
    at org.eclipse.jetty.server.Server.doStart (Server.java:383)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty (AbstractJettyMojo.java:477)
    at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute (AbstractJettyMojo.java:343)
    at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute (JettyRunMojo.java:169)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: java.lang.NoSuchMethodException: com.nextscm.proxy.config.InboundLoggingFilter.<init>()
    at java.lang.Class.getConstructor0 (Class.java:3082)
    at java.lang.Class.getDeclaredConstructor (Class.java:2178)
    at org.eclipse.jetty.server.handler.ContextHandler$Context.createInstance (ContextHandler.java:2655)
    at org.eclipse.jetty.servlet.ServletContextHandler$Context.createFilter (ServletContextHandler.java:1356)
    at org.eclipse.jetty.servlet.FilterHolder.initialize (FilterHolder.java:122)
    at org.eclipse.jetty.servlet.ServletHandler.initialize (ServletHandler.java:741)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext (ServletContextHandler.java:374)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp (WebAppContext.java:1497)
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp (JettyWebAppContext.java:360)
    at org.eclipse.jetty.webapp.WebAppContext.startContext (WebAppContext.java:1459)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart (ContextHandler.java:847)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart (ServletContextHandler.java:287)
    at org.eclipse.jetty.webapp.WebAppContext.doStart (WebAppContext.java:545)
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart (JettyWebAppContext.java:428)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:138)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:117)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:113)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart (ContextHandlerCollection.java:168)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:138)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:117)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:113)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:138)
    at org.eclipse.jetty.server.Server.start (Server.java:416)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:108)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:113)
    at org.eclipse.jetty.server.Server.doStart (Server.java:383)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty (AbstractJettyMojo.java:477)
    at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute (AbstractJettyMojo.java:343)
    at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute (JettyRunMojo.java:169)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

我确定在这个异常中引用了什么方法,因为我在不同的项目中尝试了相同的过滤器,没有任何其他方法,它工作正常。这可能是什么原因造成的?

【问题讨论】:

com.nextscm.proxy.config.InboundLoggingFilter --- 嗯... 像这样的包名,看起来就像你添加的自定义过滤器类,而 类没有无参数构造函数,这就是InboundLoggingFilter.&lt;init&gt;() 所表示的。当然,如果InboundLoggingFilter 应该是 Spring 组件,则 servlet 容器不应该调用构造函数,因此您可能错误地注册了过滤器,但是由于您没有共享此过滤器的任何代码或它是如何实现的已经注册了,我们真的无能为力。 【参考方案1】:
java.lang.NoSuchMethodException: com.nextscm.proxy.config.InboundLoggingFilter.<init>()

这告诉我,InboundLoggingFilter 类没有空构造函数。也许检查您的对象和文档的实现。但是,如果没有代码,将很难为您提供帮助。

【讨论】:

是的,这似乎是问题所在。我添加了一个空白的空构造函数,它现在似乎正在工作

以上是关于java.lang.NoSuchMethodException 尝试使用 OncePerRequestFilter [重复]的主要内容,如果未能解决你的问题,请参考以下文章