0

I am using play 2.4 and would like to set an reference object "document" in model "user". If I iterate over iterator, I get the following error: So if I call the route /users, where normally I should get an Json list with all user objects and embedded documents.

[MarshallingException: Unable to unmarshall result to class models.User from content { "_id" : { "$oid" : "560969b4d4c6209ff2a1a973"} , "name" : "Alexander Buder" , "documents" : [ { "name" : "Sample.pdf"} , { "name" : "Test.pdf"}]}]

Model User

package models;


import com.fasterxml.jackson.annotation.JsonProperty;
import org.jongo.MongoCollection;
import org.bson.types.ObjectId;
import uk.co.panaxiom.playjongo.PlayJongo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by abuder on 28.09.15.
 */
public class User {

    public static MongoCollection users() {
        return PlayJongo.getCollection("users");
    }

    @JsonProperty("_id")
    private ObjectId id;
    private String name;
    private List<Document> documents = new ArrayList<Document>();

    public User save() {
        users().save(this);
        return this;
    }

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Document> getDocuments() {
        return documents;
    }

    public void setDocuments(List<Document> documents) {
        this.documents = documents;
    }

    public void remove() {
        users().remove(this.id);
    }

    public static User findByName(String name) {
        return users().findOne("{name: #}", name).as(User.class);
    }

    public static List<User> findAll(){
        List<User> userList = new ArrayList<User>();
        Iterator<User> iterator = users().find().as(User.class).iterator();
        while (iterator.hasNext()){
            userList.add(iterator.next());
        }
        return userList;
    }

}

Model Document

package models;

/**
 * Created by abuder on 28.09.15.
 */
public class Document {


    private String name;
    public Document(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Controller

package controllers;

import models.Document;
import models.User;
import play.*;
import play.libs.Json;
import play.mvc.*;

import views.html.*;

import java.util.List;

public class Application extends Controller {

    public Result index() {

        Document doc1 = new Document("Sample.pdf");
        Document doc2 = new Document("Test.pdf");

        User user = new User();
        user.setName("Alexander Buder");
        user.getDocuments().add(doc1);
        user.getDocuments().add(doc2);
        user.save();

        return ok(Json.toJson(user));
    }


    public Result usersJson(){
       List<User> userList = User.findAll();
        return ok(Json.toJson(userList));
    }

}

Routes

GET        /                    controllers.Application.index()

GET        /users               controllers.Application.usersJson()
abuder
  • 1,030
  • 2
  • 13
  • 31

1 Answers1

0

I found the problem. I forgot to implement the default constructor in booth models, after implementing, the program works fine.

abuder
  • 1,030
  • 2
  • 13
  • 31