java - GAE 应用程序将 Long 序列化为字符串。如何改变呢?

标签 java google-app-engine java-8 google-cloud-endpoints-v2

我有一个配置了 Cloud Endpoints Framework v2 和 Java 8 的 GAE 应用程序。它对于已定义的服务运行良好。

我有一种定义新服务的方法。看起来像这样:

@ApiMethod(httpMethod = "get", path = "/operations/mine")
public UserOperationsResponse getOperationsForUser(User user) {
    return operationsService.
        getOperationsByUserId(user.getUserId());
}

这是UserOperationsResponse类的设计:

public class UserOperationsResponse {
    private List<Long> items;

    public UserOperationsResponse() {
    }

    public UserOperationsResponse(List<Long> items) {
        this.items = items;
    }

    public List<Long> getItems() {
        return items;
    }
}

该方法按预期工作并检索数据。但是,当我去 postman 并测试服务时,我得到以下响应:

{
    "items": [
        "4676209648599040"  // <-- string
    ]
}

如何在响应中获取数字数组而不是字符串数组?

最佳答案

来自:https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/users/User.html#getFederatedIdentity--

java.lang.String    getUserId()

返回一个不透明字符串,唯一标识此 User 对象表示的用户。

我想你想要:

user.getKey().getId();

关于java - GAE 应用程序将 Long 序列化为字符串。如何改变呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49678768/

相关文章:

java - 当使用不相关的接口(interface)类型调用时,为什么编译器会选择带有类类型参数的泛型方法?

java - 基本java代码错误

google-app-engine - 如何下载 Google App Engine 上的所有数据存储实体?

java - 从 Arraylist 中提取不同字符串的更快方法

python - 将 Twilio 短信正文转换为字符串 - @ 符号的编码错误

javascript - 如何将电子邮件添加到 Stripe 自定义收据表单(错误 : Received unknown parameter: email)

list - 将 TreeMap<String,Object> 转换为 List<HashMap<String,Object>>

java - “log4j:configuration” 必须匹配 “(renderer….loggerFactory)?)”

Java、MySQL 连接被输入数据终止

java - 发生 Tomcat 关闭时如何进行最终的数据库推送?