How do I use java.time.LocalDate on JSON Rests?

1,778 views
Skip to first unread message

Frederico Ferrao

unread,
Sep 30, 2015, 4:48:44 PM9/30/15
to play-framework
Hey guys,

I'm having problem trying to use java.time.LocalDate on my 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()));
       }


jackson cant instantiate LocalDate:
java.lang.RuntimeException:     com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate     value of type [simple type, class java.time.LocalDate] from String value     ('2015-09-30'); no single-String constructor/factory method
 at [Source: N/A; line: -1, column: -1] (through reference chain:     models.Person["dayOfBirth"])


What am I missing?



Thanks

Frederico Ferrao

unread,
Oct 1, 2015, 11:20:58 AM10/1/15
to play-framework
I found the problem and a workaround.

Opened an issue on github with details:  https://github.com/playframework/playframework/issues/5140

workaround is to register the properly modules on Global.java

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);
    }
}



On Wednesday, September 30, 2015 at 4:48:44 PM UTC-4, Frederico Ferrao wrote:
Hey guys,

I'm having problem trying to use java.time.LocalDate on my 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(
...
Reply all
Reply to author
Forward
0 new messages