I am working on a spring boot and hibernate project
One of my entity class has a localDate and localTime both for different purposes
public class User{
private LocalTime dailyStartTime;
private LocalDate UniversityStartDate;
//There are other fields here as well
}
My jsp form looks like this
<frm:form modelAttribute="user">
<frm: input type="time" path="dailyStartTime">
<frm: input type="date" path="UniversityStartDate">
</frm:form
Using #InitBinder, I am able parse the date and the date gets updated in the user object.
but I am not able to parse the time getting error as typeMismatch (Failed to convert property value of String to LocalTime)
There just was a similar question here JpaRepository SQL syntax error when trying to save to MySQL Date.
For the form binding you probably need to add #DateTimeFormat(pattern = "yyyy-MM-dd") and #DateTimeFormat(pattern = "HH-mm"). However it's good practice to separate the data object from the actual entities, otherwise you may get problems persisting with the new attributes.
I'm trying to generate the documentation from my springboot application using spring doc , this is some of the attributes of the class which is causing me issues:
public class user {
#JsonFormat(pattern = "yyyy-MM-dd")
private Date dateOfBirth;
}
With the Spring doc annotation, in the swagger i got this:
dateOfBirth* string($date-time)
"dateOfBirth": "2020-04-29T14:15:32.475Z"
while i would like to have this:
dateOfBirth* string($date)
"dateOfBirth": "2020-04-29"
How to do that? I think to be close to solution but i can't firugre out what i'm missing
I think the answer you are looking for is here: swagger date field vs date-time field
Date is an object DateTime for swagger, as it is really a DateTime object. Use the appropriate type, like LocalDate, they know how to handle that.
By the way, how would you expect Swagger to properly convert a Date Pattern into the appropriate type ? It's like too much magic. Swagger relies on thing that are common practices.
The JSONFormat won't change how swagger interpret your data.
Spring boot 2 has made UTC format the default for dates when serializing objects as json. This broke several of our old integrations that relied on the date being a timestamp. How do I selectively restore this functionality to the responses that need it?
On any dates you need formatted as a timestamp again, in either the constructor or on the field annotate them with #JsonFormat(shape = JsonFormat.Shape.NUMBER) like so:
#JsonFormat(shape = JsonFormat.Shape.Number)
private Date myDate;
or
MyClass(#JsonFormat(shape = JsonFormat.Shape.Number)
Date myDate) {
...
}
I am using spring-boot and I have an entity class defined something like this
import org.joda.time.LocalDateTime;
#Entity
public class Project {
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime start_date;
...
...
}
When this class is converted to JSON, the field gets converted to the following string representation
{"start_date":[2014,11,15,0,0,0,0],...., ...}
I want to have the json response as yyyy-MM-dd.
I tried the #DateTimeFormat(iso = ISO.DATE) annotation and that did not help either.
Is there an easy way to do this conversion to proper json format ?
There are three things that you need to do to format the date as yyyy-MM-dd:
Add a dependency on com.fasterxml.jackson.datatype:jackson-datatype-joda. Judging by the output you're getting at the moment, I think you may already have this dependency.
Configure Jackson not to format dates as timestamps by adding spring.jackson.serialization.write-dates-as-timestamps: false to your application.properties file.
Annotate the LocalDataTime field or getter method with #JsonFormat(pattern="yyyy-MM-dd")
Note: You'll need to use Spring Boot 1.2 for step 2 to work.
Without additional dependency - the only thing I had to do is:
To take care send date from client as string object, in format yyyy/MM/dd
In Spring Boot application, to add annotation on the date field with
the same format
public class Foo
{
#JsonFormat(pattern = "yyyy/MM/dd")
private Date dueDate;
}
Using Spring Boot 2.3.5 version
Update
Another option, instead of step 2, to modify application.properties file, add there the format for any Date object:
spring.jackson.date-format=yyyy/MM/dd
You can use #JsonFormat annotation in and the desired pattern like this without using any dependency :
#JsonFormat(pattern="yyyy-MM-dd")
private Date created_At;
Took me some time struggling with Spring Boot Application + Date Format for my input so I'll try to resume what I saw.
If your date is argument to a function, you can use #DateTimeFormat(pattern = "yyyy-MM-dd") to define a pattern (ie. org.springframework.format.annotation.DateTimeFormat).
If your date is inside an object argument to the function, you can use #JsonFormat(pattern = "yyyy-MM-dd") to define a pattern (ie. com.fasterxml.jackson.annotation.JsonFormat)
If neither of these works, you can try changing your date Type, for me I had tu use org.joda.time.LocalDate in order to make it work with option 2 :
#JsonFormat(pattern = "dd/MM/yyyy")
private org.joda.time.LocalDate date;
I'm using Spring's #DateTimeFormat annotation in order to parse the output of toISOString() from javascript:
#RequestMapping(method=RequestMethod.GET)
public #ResponseBody List<Entity> search(#RequestParam("date") #DateTimeFormat(pattern="\"yyyy-MM-dd'T'HH:mm:ss.SSSZ\"") Date date) {
return service.search(date);
}
The URL ends up looking like: ?date="2014-07-14T19:19:33.625Z" which seems correct. #DateTimeFormat is not correctly parsing the timezone, it seems like it wants UTC to be represented as '-0000' instead of 'Z'
Am I doing something wrong? You would expect #DateTimeFormat(iso=ISO.DATE_TIME) to work with javascript toISOString(), except js is adding the quotes and 'Z' doesn't seem to work.
This is Spring 3.1.