简体   繁体   English

如何在发布请求-Google Cloud Endpoints中修复NullPointerException?

[英]How to fix NullPointerException in post request - Google Cloud Endpoints?

I am trying to create an api method on google endpoint where I am receiving the data in the request body which I want save in the datastore. 我正在尝试在Google端点上创建一个api方法,在其中我要在请求主体中接收要保存在数据存储区中的数据。

However, I get the following response when I make a request: 但是,发出请求时会收到以下响应:

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "backendError",
                "message": "java.lang.NullPointerException"
            }
        ],
        "code": 503,
        "message": "java.lang.NullPointerException"
    }
}

Please refer the api method below: 请参考下面的api方法:

@ApiMethod(name="profile",
               path="profile",
               httpMethod=HttpMethod.POST)
    public UserProfile addUser(UserProfile userProfile) {

        userProfileService.addUser(userProfile);
        return userProfile;
    }

Below is my dao class implementation: 下面是我的dao类的实现:

package com.travelplannr.endpoint.dao;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.travelplannr.endpoint.entities.UserProfile;

public class UserProfileDaoImpl implements UserProfileDao {

    static {
        factory().register(UserProfile.class);
    }

    public static Objectify ofy() {
        return ObjectifyService.ofy();
    }

    public static ObjectifyFactory factory() {
        return ObjectifyService.factory();
    }

    @Override
    public void addUser(UserProfile userProfile) {

        UserProfileDaoImpl.ofy().save().entity(userProfile).now();
    }

}

Please refer Entity class below: 请参考下面的实体类:

package com.travelplannr.endpoint.entities;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class UserProfile {

    @Id
    private String email;
    private String name;
    private String password;

    public UserProfile() {}

    public UserProfile(String name, String email, String password) {
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserProfile [name=" + name + ", email=" + email + ", password=" + password + "]";
    }
}

Below is the stack trace: 下面是堆栈跟踪:

java.lang.NullPointerException
    at com.travelplannr.endpoint.api.TravelPlannrApi.addUser(TravelPlannrApi.java:34)
    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 com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:129)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:351)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:119)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:102)
    at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:49)
    at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:72)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerRequestLogFilter.doFilter(DevAppServerRequestLogFilter.java:44)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:95)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:508)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

I have also checked removing the call to UserProfileService.add in the api method and just returning the object that I receive in the request body and it is not empty. 我还检查了在api方法中删除对UserProfileService.add的调用,只是返回了我在请求正文中收到的对象,并且该对象不为空。

Please help! 请帮忙! Thanks in advance. 提前致谢。

The context missing from your post is how userProfileService is initialized or expected to get set in the first place (which is outside the scope of this ApiMethod). 帖子中缺少的上下文是如何初始化或首先设置userProfileService方式(这不在本ApiMethod的范围之内)。 If I'm understanding correctly, this doesn't seem like an issue with Endpoints at all; 如果我正确理解的话,这似乎根本就不是端点的问题; that's just a red herring. 那只是一条红鲱鱼。

It may help speed up your debugging by temporarily removing the Endpoints integration and making sure all of your object references are being created correctly by whatever injection framework/method you're using to wire things up. 通过临时删除Endpoints集成并确保通过任何用于连接事物的注入框架/方法正确创建所有对象引用,可以帮助加快调试速度。 Maybe create a small main() method that triggers your object bootstrapping and checks to ensure this userProfileService is non-null? 也许创建一个小的main()方法来触发您的对象引导并检查以确保此userProfileService不为空? That would allow you to iterate much faster in a command-line environment until you figure out the root cause of the initialization failure before you try to get Endpoints integration working. 这样一来,您便可以在命令行环境中更快地进行迭代,直到在尝试使Endpoints集成正常工作之前找出初始化失败的根本原因。

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

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