【问题标题】:Endpoints method not working, why?端点方法不起作用,为什么?
【发布时间】:2016-10-04 11:51:00
【问题描述】:

当我在 api explorer 中测试我的 google 端点 API 时,saveProfile 方法会引发 503 Service Unavailable 错误,但所有其他方法都可以正常工作。

这是我的端点中的 saveProfile 方法:

@ApiMethod(name = "saveProfile")
public Profile saveProfile(final ProfileForm profileForm) {
    String firstName = profileForm.getFirstName();
    String lastName = profileForm.getLastName();
    String email = profileForm.getEmail();

    Profile profile = new Profile("124234132", email, firstName, lastName);

    ofy().save().entity(profile).now();

    return profile;
}

这是 Profile 实体 类:

@Entity
public class Profile {

    @Id
    private String userId;

    private String email;

    private String firstName;

    private String lastName;

    public Profile(String userId, String email, String firstName, String lastName) {
        this.userId = userId;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

这是 profileForm 类:

public class ProfileForm {

    private String firstName;

    private String lastName;

    private String email;

    public ProfileForm() {}

    public ProfileForm(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEmail() {
        return email;
    }
}

我已经注册了实体并设置了API_EXPLORER_CLIENT_ID 以及 web 和 android 客户端 ID。

有人知道如何解决这个问题,以便该方法只返回应有的配置文件对象吗?

【问题讨论】:

  • 文档说,“所有 HTTP 5xx 状态代码在客户端响应中都转换为 HTTP 503。” cloud.google.com/appengine/docs/java/endpoints/…
  • 您应该在console 的请求日志中找到有关发生错误的信息。
  • user2004685,那么我如何让它返回成功代码(我认为 200 表示成功)。
  • zapl,这是它所说的其中一件事:JsonMappingException: No serializer found for class com.example.tomfinet.myapplication.backend.model.Profile and no properties found to create BeanSerializer (以避免异常, 禁用 SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)

标签: java android google-app-engine google-cloud-endpoints http-status-code-503


【解决方案1】:

为了使 Profile 可序列化,您需要为其每个字段定义 getter 和 setter(例如 getUserId()、setUserId(string Id))。

Objectify 实体还必须包含无参数构造函数:https://github.com/objectify/objectify/wiki/Entities

如果这能解决错误,请告诉我。

【讨论】:

  • 即使对于带有 @Id 注释的字段?
猜你喜欢
  • 2021-12-20
  • 2023-03-03
  • 1970-01-01
  • 2013-01-22
  • 2018-08-06
  • 2012-01-14
  • 2015-11-07
  • 2015-12-10
  • 1970-01-01
相关资源
最近更新 更多