2

I have a RESTful Web Service that acts as a repository of ToDo tasks.

On the other hand I have a Web Application that uses that REST Server for managing the Todo repository.

I've tried my first call from the client to the server in order to retrieve some data, but it fails.

The code in the REST Server is as follows:

/**
 * 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;
    }

....

And the client call (in 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);
                });
        };
      }]);
....
....
})();

But the server always return a 500 (Internal Server Error)

This call it's made when you click on the "AddToDo" tab of the web application, and you can see the result of the call in the console, the error in this case.

But the most remarkable thing is that when I change the REST Server code:

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

Then, it returns correctly the string "It's me, example text".

I've checked the libaries included in the I've checked the libaries included in the build.gradle file and all the server classes over and over again, finding no solution at all.

This are the headers of the HTTP request-response, all seems in order, except for the error of course:

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

¿Anybody who maybe know where this error could come from?

6
  • 1
    How is ToDoList type defined? Commented Nov 1, 2014 at 0:53
  • 2
    Okay, you got an error 500, so, show the server log please. Commented Nov 1, 2014 at 2:52
  • Maybe Dependency Injection with @Inject is not supported by the server. Maybe look into that? Commented Nov 1, 2014 at 6:17
  • Heres is the ToDoList class @PM77-1 Commented Nov 1, 2014 at 10:18
  • I wish I could @denisazevedo but no exception is thrown by the server side Commented Nov 1, 2014 at 12:08

1 Answer 1

3

The problem lies in your POJO. You must provide a zero argument constructor to the ToDo class. The MOXy marshaller from POJO to JSON requires a zero argument constructor.

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 ...}
}
Sign up to request clarification or add additional context in comments.

1 Comment

@J. Lopez-pellicer. Could you see this link. stackoverflow.com/questions/47336295/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.