Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class models.Job] from JSON String

i use the playframework and tried to deserialize some json into a java object. It worked fine, exept the relationship in the model. I got the following exception

enter code hereorg.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class models.Job] from JSON String; no single-String constructor/factory method (through reference chain: models.Docfile["job"])

i thought jackson in combination with play could do that:

this is the json

{"name":"asd","filepath":"blob","contenttype":"image/png","description":"asd","job":"1"}

and this my code, nothing special:

public static Result getdata(String dataname) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            Docfile docfile = mapper.readValue((dataname), Docfile.class);
            System.out.println(docfile.name);
            docfile.save();

        } catch (JsonGenerationException e) {

            e.printStackTrace();

        } catch (JsonMappingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return ok();
    }

Hope there is help for me, thanks Markus

UPDATE:

Docfile Bean:

package models;

import java.util.*;

import play.db.jpa.*;
import java.lang.Object.*;
import play.data.format.*;
import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import play.data.validation.Constraints.*;
import play.data.validation.Constraints.Validator.*;

import javax.persistence.*;

import com.avaje.ebean.Page;

@Entity
public class Docfile extends Model {

    @Id
    public Long id;

    @Required
    public String name;

    @Required
    public String description;

    public String filepath;

    public String contenttype;

    @ManyToOne
    public Job job;

    public static Finder<Long,Docfile> find = new Model.Finder(
            Long.class, Docfile.class
            );




    public static List<Docfile> findbyJob(Long job) {
        return find.where()
                .eq("job.id", job)
                .findList();
    }

    public static Docfile create (Docfile docfile, Long jobid) {
        System.out.println(docfile);
        docfile.job = Job.find.ref(jobid);
        docfile.save();
        return docfile;
    }
}
like image 684
Markus Pleines Avatar asked Jun 20 '12 20:06

Markus Pleines


2 Answers

Either you change your JSON in order to describe your "job" entity :

{
   "name":"asd",
   "filepath":"blob",
   "contenttype":"image/png",
   "description":"asd",
   "job":{
      "id":"1",
       "foo", "bar"
   }
}

or you create a constructor with a String parameter in your Job bean:

public Job(String id) {
// populate your job with its id
}
like image 162
ndeverge Avatar answered Nov 29 '22 05:11

ndeverge


when limited time +ee: +jax-rs && +persistence, +gson; I have solved it then as:

@Entity
@XmlRootElement
@Table(name="element")
public class Element implements Serializable {
    public Element(String stringJSON){
        Gson g = new Gson();
        Element a = g.fromJson(stringJSON, this.getClass());
        this.setId(a.getId());
        this.setProperty(a.getProperty());
    }

    public Element() {}
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    ...
}
like image 22
Oleksii Kyslytsyn Avatar answered Nov 29 '22 05:11

Oleksii Kyslytsyn