简体   繁体   中英

Spring Boot - Failed to initialize ServletRegistrationBean in WebLogic 12c

We're running GraphQL sample Java application in WebLogic 12c, but encountered some errors with servlet during startup.

The servlet was configured and register our servlets using a ServletRegistrationBean as below:

@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;
    }
}

In the weblogic server log, it prints out errors:

<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

It looks that the weblogic is trying to create a new servlet instance instead of the ServletBean SimpleGraphQLServlet created and managed by Spring context.

Any advance? Thanks.

A walkaround solution is to write a Servlet wrapper class like this:

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);
     }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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