playframework - How to use java.time.LocalDate on a Play Framework JSON Rest? -


i'm trying, without success, make play serialize , deserialize java 8 localdate.

on play 2.4, it's said work out of box:

play 2.4 requires jdk 8. due this, play can, out of box, provide support java 8 data types. example, play’s json apis support java 8 temporal types including instance, localdatetime , localdate.

but i'm unable make work.

here have: i'm using play 2.4.3, playjava , playebean plugins

model

@entity public class person extends model {      @id     public long id;      public string name;      public localdate dayofbirth;      @jsonformat(shape = jsonformat.shape.string, pattern = "dd/mm/yyyy")     public localdate anotherdate;      @formats.datetime(pattern = "dd/mm/yyyy")     public localdate andanotherone;      public static model.finder<long, person> find = new model.finder<long, person>(person.class); } 

controller

public class personcontroller extends controller {      public result create() {         person person = json.fromjson(request().body().asjson(), person.class);          person.save();          return ok(json.tojson(person));     }      public result all() {         return ok(json.tojson(person.find.all()));     } } 

routes

post        /person              @controllers.personcontroller.create()          /person              @controllers.personcontroller.all() 

i test chrome app rest , got error in 3 forms:

{"name":"fred","dayofbirth":"2015-09-30"} {"name":"fred","anotherdate":"30/09/2015"}   {"name":"fred","andanotherone":"30/09/2015"} 

error

java.lang.runtimeexception:     com.fasterxml.jackson.databind.jsonmappingexception: can not instantiate     value of type [simple type, class java.time.localdate] string value     ('2015-09-30'); no single-string constructor/factory method  @ [source: n/a; line: -1, column: -1] (through reference chain:     models.person["dayofbirth"]) 

what missing?

update: sample app: https://github.com/fredferrao/localdatejsontest

i found problem, play injecting custom objectmapper , think it's not registering jdk8module , jsr310module of jackson.

i opened issue

the workaround found set own objectmapper on global.java

import com.fasterxml.jackson.databind.deserializationfeature; import com.fasterxml.jackson.databind.objectmapper; import com.fasterxml.jackson.datatype.jdk8.jdk8module; import com.fasterxml.jackson.datatype.jsr310.jsr310module; import play.application; import play.globalsettings; import play.libs.json;  /**  * criado por ferrao em 01/10/2015.  */ public class global extends globalsettings {     public void onstart(application app) {         objectmapper mapper = new objectmapper();         mapper.registermodule(new jdk8module());         mapper.registermodule(new jsr310module());         mapper.configure(deserializationfeature.fail_on_unknown_properties, false);         json.setobjectmapper(mapper);     } } 

the mapper.configure part copied play.libs.json source


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -