java - 部署时我的一种云端点方法出现 500 Internal Server Error

标签 java google-app-engine rest google-cloud-endpoints

我的所有 Cloud Endpoints 方法都在本地工作,并且除了一个以外的所有方法都在部署应用程序时工作。方法如下:

@ApiMethod(name = "listUrl", path ="article/urls", httpMethod = HttpMethod.GET)
public String[] listUrl() {
    List<Article> articles = getArticleList();
    String[] urlArray = new String[articles.size()];
    int i = 0;
    for (Article article : articles)
        urlArray[i++] = article.getUrl(); 
    return urlArray;
}

我知道接受的三种返回类型是“POJO、数组或集合”。但我认为可能是 String[] 导致了错误,因此尝试返回 String 的集合。

@ApiMethod(name = "listUrl", path ="article/urls", httpMethod = HttpMethod.GET)
public CollectionResponse<String>/*String[]*/ listUrl() {
    List<Article> articles = getArticleList();
    List<String> urls = new ArrayList<String>();
    //String[] urlArray = new String[articles.size()];
    //int i = 0;
    for (Article article : articles)
        urls.add(article.getUrl());
        //urlArray[i++] = article.getUrl(); 
    return /*urlArray*/ CollectionResponse.<String> builder().setItems(urls).build();
}

但是没有用。我继续收到“500 内部服务器错误”。

500: Internal Server Error最奇怪的部分是,当我查看管理控制台中的日志时,它指出请求顺利通过:

2013-09-22 22:08:00.715 /_ah/spi/com.example.hiserver.ArticleEndpoint.listUrl 200 55ms 0kb Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20100101 Firefox/23.0

xx.xxx.xx.xxx(my IP) - - [22/Sep/2013:22:08:00 -0700] "POST /_ah/spi/com.example.hiserver.ArticleEndpoint.listUrl HTTP/1.1" 200 129 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20100101 Firefox/23.0" "hylyt-it.appspot.com" ms=56 cpu_ms=21 cpm_usd=0.000014 app_engine_release=1.8.4 instance=00c61b117cdf561948b997f4ec6be2ca72c139d1

最佳答案

根据 willmas 评论,我摆弄了我的代码,发现你不能使用泛型作为返回参数。当您考虑 appengine 应该如何处理并将这些东西序列化为 json 时,这实际上是有道理的。然后当它在本地 devserver 上工作时它又不会。 无论如何,在我的例子中,我使用了一个列表,它在本地开发服务器上运行良好,但在部署时却不行。解决方案是创建一个

public class StringResult {
    private String result;
    /* getters and setters */ 
}

在你的情况下你可以使用

public class StringArrayResult {
    private String[] result;
    /* getters and setters */
}

感谢@willma 指出了正确的方向。

关于java - 部署时我的一种云端点方法出现 500 Internal Server Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18952299/

相关文章:

c# - 如何格式化Json输出?

Java/泛型/ClassCastException

Java - 找不到符号 - 构造函数

java - 需要RESTLET + JAX-RS + JSON 好例子

java.security.NoSuchAlgorithmException 在 Spring 的 RestTemplate 做请求时

java - Google App Engine Java 上的 RESTful 应用程序?

java - 将 int 数组转换为一堆 int

java - 我可以以非模态方式使用 Java JOptionPane 吗?

python - IOError : [Errno 13] file not accessible with Google AppEngine 1. 6.1

google-app-engine - 下载 App Engine 源代码