繁体   English   中英

Spring Boot-无法在WebLogic 12c中初始化ServletRegistrationBean

[英]Spring Boot - Failed to initialize ServletRegistrationBean in WebLogic 12c

我们正在WebLogic 12c中运行GraphQL示例Java应用程序,但是在启动过程中servlet遇到了一些错误。

如下所示,已配置servlet并使用ServletRegistrationBean注册我们的servlet:

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnBean({GraphQLSchema.class, GraphQLSchemaProvider.class})
@ConditionalOnProperty(value = "graphql.servlet.enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureAfter({GraphQLJavaToolsAutoConfiguration.class, SpringGraphQLCommonAutoConfiguration.class})
@EnableConfigurationProperties(GraphQLServletProperties.class)

public class GraphQLWebAutoConfiguration {
    ...
    @Bean
    @ConditionalOnMissingBean
    public GraphQLServlet graphQLServlet(GraphQLSchemaProvider schemaProvider, ExecutionStrategyProvider executionStrategyProvider) {
        return new SimpleGraphQLServlet(schemaProvider, executionStrategyProvider, listeners, instrumentation, errorHandler, contextBuilder);
    }

    @Bean
    ServletRegistrationBean graphQLServletRegistrationBean(GraphQLServlet servlet) {
        ServletRegistrationBean bean = new ServletRegistrationBean(servlet, graphQLServletProperties.getServletMapping());
        bean.setLoadOnStartup(1);
        return bean;
    }
}

在weblogic服务器日志中,它输出错误:

<Oct 2, 2017 1:51:28 PM SGT> <Error> <HTTP> <BEA-101125> <[ServletContext@933807824[app:CPRES module:CPRES path:null spec-version:3.1]] Error occurred while instantiating servlet: "simpleGraphQLServlet".
java.lang.InstantiationException: graphql.servlet.SimpleGraphQLServlet
        at java.lang.Class.newInstance(Class.java:427)
        at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributor.java:252)
        at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributor.java:245)
        at weblogic.servlet.internal.WebComponentContributor.createServletInstance(WebComponentContributor.java:274)
        at weblogic.servlet.internal.StubSecurityHelper$ServletInitAction.newServletInstanceIfNecessary(StubSecurityHelper.java:365)
        Truncated. see log file for complete stacktrace
Caused By: java.lang.NoSuchMethodException: graphql.servlet.SimpleGraphQLServlet.<init>()
        at java.lang.Class.getConstructor0(Class.java:3082)
        at java.lang.Class.newInstance(Class.java:412)
        at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributor.java:252)
        at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributor.java:245)
        at weblogic.servlet.internal.WebComponentContributor.createServletInstance(WebComponentContributor.java:274)
        Truncated. see log file for complete stacktrace
>
<Oct 2, 2017 1:51:28 PM SGT> <Error> <HTTP> <BEA-101216> <Servlet: "simpleGraphQLServlet" failed to preload on startup in Web application: "CPRES".
javax.servlet.ServletException: Servlet class: 'graphql.servlet.SimpleGraphQLServlet' couldn't be instantiated
        at weblogic.servlet.internal.StubSecurityHelper$ServletInitAction.run(StubSecurityHelper.java:326)
        at weblogic.servlet.internal.StubSecurityHelper$ServletInitAction.run(StubSecurityHelper.java:294)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
        at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:196)
        at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
        Truncated. see log file for complete stacktrace
>
<Oct 2, 2017 1:51:29 PM SGT> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID "19094957742917053" for task "130" on [partition-name: DOMAIN]. Error is: "weblogic.application.ModuleException: javax.servlet.ServletException: Servlet class: 'graphql.servlet.SimpleGraphQLServlet' couldn't be instantiated"
weblogic.application.ModuleException: javax.servlet.ServletException: Servlet class: 'graphql.servlet.SimpleGraphQLServlet' couldn't be instantiated

看起来weblogic正在尝试创建一个新的servlet实例,而不是由Spring上下文创建和管理的ServletBean SimpleGraphQLServlet。

有进步吗? 谢谢。

解决方法是编写一个Servlet包装器类,如下所示:

public class DelegateGraphQLServlet extends HttpServlet {
    protected GraphQLServlet graphQLServlet;

    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);

        ApplicationContext ac = (ApplicationContext) servletConfig.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        this.graphQLServlet = (GraphQLServlet)ac.getBean("graphQLServlet");
    }

     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
         graphQLServlet.service(req, res);
     }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM