Angularjs 对 REST 服务器的 GET http 请求返回 500(内部服务器错误)

标签 angularjs rest http jax-rs

我有一个RESTful Web Service它充当 ToDo 任务的存储库。

另一方面,我有一个 Web Application使用该 REST 服务器来管理 Todo 存储库。

我尝试了从客户端到服务器的第一次调用以检索一些数据,但失败了。

REST Server中的代码如下:

/**
 * A service that manipulates ToDo's in an repository.
 *
 */
@Path("/todolist")
public class ToDoWebService {

    /**
     * The (shared) repository object. 
     */
    @Inject
    ToDoList toDoList;

    /**
     * A GET /todos request should return the ToDo's repository in JSON.
     * @return a JSON representation of the ToDo's repository.
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ToDoList getToDoList() {
        return toDoList;
    }

....

客户端调用(在 AngularJS 中):

(function() {
      var app = angular.module('toDoApp', []);

      var API_URI = "http://localhost:8080/todolist";

      ....
      ....

      app.controller('addToDoCtrl', ['$scope', '$http', function($scope, $http){
        $scope.getToDoList = function() {
            $http.get(API_URI).
                success(function(data) {
                    console.log(data);
                }).
                error(function(data, status, headers, config) {
                    console.log(status);
                    console.log(data);
                    console.log(headers);
                    console.log(config);
                });
        };
      }]);
....
....
})();

但服务器总是返回500(内部服务器错误)

当您单击 Web 应用程序的“AddToDo”选项卡时,就会进行此调用,您可以在控制台中看到调用的结果,在本例中是错误。

但最值得注意的是,当我更改 REST 服务器代码时:

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getToDoList() {
        return "It's me, example text";
    }

然后,它正确返回字符串“It's me, example text”。

我已经检查了包含在 我已经检查了 build.gradle 中包含的库文件和所有server classes翻来覆去,始终没有找到解决办法。

这是 HTTP 请求响应的 header ,一切似乎都按顺序进行,当然除了错误:

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/todolist
Request Method:GET
Status Code:500 Internal Server Error
**Request Headers**
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost
Referer:http://localhost/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:origin, content-type, accept, authorization
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS, HEAD
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1209600
Connection:close
Content-Length:0
Date:Fri, 31 Oct 2014 23:45:08 GMT

有人知道这个错误可能来自哪里吗?

最佳答案

问题出在你的 POJO 上。您必须向 ToDo 类提供一个零参数构造函数。从 POJO 到 JSON 的 MOXy 编码器需要零参数构造函数。

public class ToDo {

  private String task;      //Task description
  private String context;   //Task context
  private String project;   //Task project
  private int priority; //Task priority
  private URI href;
  private int id;

  // JUST ADD THIS!!!!
  public ToDo(){
  }

  //CONSTRUCTOR
  public ToDo(String task,String ctx,String proj,int pri){
    this.task = task;
    this.context = ctx;
    this.project = proj;
    this.priority = pri;
  }

  {... your code continue here ...}
}

关于Angularjs 对 REST 服务器的 GET http 请求返回 500(内部服务器错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685207/

相关文章:

http - Angular 2 和 karma 测试和异步 HTTP

ios - 检查 url 是否存在而不检查 HTTP 响应

angularjs - 使用 Angular JS 调用 Restful API 的跨域问题

angularjs - 如何在浏览器中控制缓存

angularjs - $compile 与 $componentController

javascript - Angular Service 在返回 Promise 时抛出异常

用于启用和禁用的 RESTFul 模式 URL

java - 休息Web服务参数问题

linux - 发送 HTTP 请求并接收输出。 x86 Linux 汇编

php - 在 Codeigniter PHP 中更新查询使 MySQL 中的记录为空