Passing in a LocalDate to a REST endpoint

1,116 views
Skip to first unread message

Matt Raible

unread,
Oct 3, 2016, 5:14:44 PM10/3/16
to JHipster dev team
Hey guys,

When I created my 21-Points application with a 2.x version of JHipster, I was able to use JodaTime and the following worked:

/**
* GET /bp-by-days -> get all the blood pressure readings for a particular month.
*/
@RequestMapping(value = "/bp-by-month/{date}")
@Timed
public ResponseEntity<BloodPressureByPeriod> getByMonth(@PathVariable @DateTimeFormat(pattern = "yyyy-MM") LocalDate date) {
LocalDate firstDay = date.dayOfMonth().withMinimumValue();
LocalDate lastDay = date.dayOfMonth().withMaximumValue();

With the latest version, I’m using the the Java Time API and trying something similar:

public ResponseEntity<BloodPressureByPeriod> getByMonth(@PathVariable @DateTimeFormat(pattern = "yyyy-MM") LocalDate date) {
LocalDate firstDay = date.withDayOfMonth(1);
LocalDate lastDay = date.withDayOfMonth(date.lengthOfMonth());

However, this results in an error:

2016-10-03 15:13:39.744 WARN 92872 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type [java.lang.String] to required type [java.time.LocalDate]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] for value '2016-09'; nested exception is java.time.format.DateTimeParseException: Text '2016-09' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2016, MonthOfYear=9},ISO of type java.time.format.Parsed

MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/bp-by-month/2016-09
Parameters = {}
Headers = {}

Handler:
Type = org.jhipster.health.web.rest.BloodPressureResource
Method = public org.springframework.http.ResponseEntity<org.jhipster.health.web.rest.dto.BloodPressureByPeriod> org.jhipster.health.web.rest.BloodPressureResource.getByMonth(java.time.LocalDate)

Async:
Async started = false
Async result = null

Resolved Exception:
Type = org.springframework.web.method.annotation.MethodArgumentTypeMismatchException

Thanks in advance for any ideas.

Matt

Christophe Bornet

unread,
Oct 4, 2016, 4:00:15 AM10/4/16
to JHipster dev team
Hi Matt,

Have you tried using a java.time.YearMonth instead ?

Regards.

Matt Raible

unread,
Oct 4, 2016, 11:37:50 AM10/4/16
to Christophe Bornet, JHipster dev team
Thanks Christophe - that works!

Something related to dates I discovered this morning - the following two dependencies depend on Joda Time, so it can be confusing when working with dates and imports.

org.springframework.boot:spring-boot-starter-data-elasticsearch
fr.ippon.spark.metrics:metrics-spark-reporter

I tried excluding joda-time from these dependencies, but then my app fails to start.

Cheers,

Matt


--
You received this message because you are subscribed to the Google Groups "JHipster dev team" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jhipster-dev...@googlegroups.com.
To post to this group, send email to jhipst...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jhipster-dev/fc72d28c-0a49-41d9-ac6e-d68ac291df71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages