Google云端点和依赖注入

时间:2022-03-31 20:20:27

I have an issue when I inject a service in my endpoint class.

我在端点类中注入服务时遇到问题。

[INFO] java.lang.RuntimeException: Cannot instantiate service class: fr.cloudintra.endpoints.endpoint.VacationRequestEndPoint
[INFO]  at com.google.api.server.spi.SystemServiceServlet.createService(SystemServiceServlet.java:242)
[INFO]  at com.google.api.server.spi.SystemServiceServlet.registerService(SystemServiceServlet.java:229)
[INFO]  at com.google.api.server.spi.SystemServiceServlet.createSystemService(SystemServiceServlet.java:222)
[INFO]  at com.google.api.server.spi.SystemServiceServlet.createSystemService(SystemServiceServlet.java:184)
[INFO]  at com.google.api.server.spi.SystemServiceServlet.init(SystemServiceServlet.java:106)
[INFO]  at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
[INFO]  at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
[INFO]  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
[INFO]  at com.googlecode.objectify.cache.AsyncCacheFilter.doFilter(AsyncCacheFilter.java:58)
[INFO]  at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:127)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
[INFO]  at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
[INFO]  at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
[INFO]  at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
[INFO]  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
[INFO]  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
[INFO]  at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
[INFO]  at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
[INFO]  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
[INFO]  at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:503)
[INFO]  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
[INFO]  at org.mortbay.jetty.Server.handle(Server.java:326)
[INFO]  at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
[INFO]  at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
[INFO]  at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
[INFO]  at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
[INFO]  at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
[INFO]  at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
[INFO]  at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
[INFO] Caused by: java.lang.InstantiationException: fr.cloudintra.endpoints.endpoint.VacationRequestEndPoint.<init>()

Guice is used to manage dependency injection.

Guice用于管理依赖注入。

The endpoint class:

端点类:

public class VacationRequestEndPoint {

    private VacationRequestGateway vacationRequestGateway;

    @Inject
    public VacationRequestEndPoint(VacationRequestGateway vacationRequestGateway) {
        this.vacationRequestGateway = vacationRequestGateway;
    }

    @ApiMethod(name = "list", httpMethod = ApiMethod.HttpMethod.GET, path = "list")
    public Collection<VacationRequest> getVacationRequests() {
        return vacationRequestGateway.findVacationRequests();
    }
}

The gateway:

网关:

public class VacationRequestGateway {
    {
        ObjectifyService.register(VacationRequest.class);
    }

    public Collection<VacationRequest> findVacationRequests() {
        return ofy().load().type(VacationRequest.class).list();
    }
}

And the end point test class (which pass):

和终点测试类(传递):

@RunWith(MockitoJUnitRunner.class)
public class VacationRequestEndPointTest {

    @Mock
    private VacationRequestGateway mockGateway;

    @InjectMocks
    private VacationRequestEndPoint endPoint;

    @Test
    public void testGetVacationRequests() throws Exception {
        endPoint.getVacationRequests();

        verify(mockGateway).findVacationRequests();
    }
}

And don't know how I can inject my gateway on my endpoint. Do I need to override the way the endpoint is initialized ?

并且不知道如何在我的端点上注入我的网关。我是否需要覆盖端点的初始化方式?

Thanks.

谢谢。

1 个解决方案

#1


1  

The problem, as konqi guessed, is that com.google.api.server.spi.SystemServiceServlet.createService() is calling java.lang.Class#createInstance() on VacationRequestEndPoint and VacationRequestEndPoint doesn't have a no-arg constructor.

正如konqi猜测的那样,问题是com.google.api.server.spi.SystemServiceServlet.createService()在VacationRequestEndPoint上调用java.lang.Class#createInstance()而VacationRequestEndPoint没有no-arg构造函数。

I don't know why you expected VacationRequestEndPoint to be instantiated by Guice, but I suspect you have a configuration error with however you registered VacationRequestEndPoint with the Google Cloud infrastructure.

我不知道你为什么期望由Guice实例化VacationRequestEndPoint,但我怀疑你有一个配置错误,但是你在Google Cloud基础架构中注册了VacationRequestEndPoint。

#1


1  

The problem, as konqi guessed, is that com.google.api.server.spi.SystemServiceServlet.createService() is calling java.lang.Class#createInstance() on VacationRequestEndPoint and VacationRequestEndPoint doesn't have a no-arg constructor.

正如konqi猜测的那样,问题是com.google.api.server.spi.SystemServiceServlet.createService()在VacationRequestEndPoint上调用java.lang.Class#createInstance()而VacationRequestEndPoint没有no-arg构造函数。

I don't know why you expected VacationRequestEndPoint to be instantiated by Guice, but I suspect you have a configuration error with however you registered VacationRequestEndPoint with the Google Cloud infrastructure.

我不知道你为什么期望由Guice实例化VacationRequestEndPoint,但我怀疑你有一个配置错误,但是你在Google Cloud基础架构中注册了VacationRequestEndPoint。