How to send Date in REST API in POST method
You can make your Custom LocalDate Deserializer. This Deserializer will be called when setter method for the LocalDate variable is called.
Steps as follows:
Define a Custom Deserializer
public class LocalDateDeserializer extends JsonDeserializer<LocalDate>{
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("required format");
LocalDate localDate = null;
localDate = LocalDate.parse(p.getText(), formatter);
return localDate;
}
}
Note: Reference for LocalDate.parse method.
Define @JsonDeserialize annotation above the variable
@JsonDeserialize(using=LocalDateDeserializer.class)
private LocalDate quantityAtDate;
For using @JsonDeserialize annotation import following:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
Hope this helps.
I am sending Date in the request Body to a REST API post request, but on checking the record inserted in the table, Timestamp is getting inserted
I am guessing that, the date stored in your database is 02-AUG-2021 00:00:00
in UTC and 20-AUG-2014 00:00:00
in UTC. Since your timezone is +0530, the time is shown as 02-AUG-2021 05:30:00
and 20-AUG-2014 05:30:00
in Indian Time Zone.
See, If your database has any data type to store only Date
like mysql's Date
or you can store plain string.
Format Date in response of POST rest API
You have to annotate the field @field:JsonSerialize
and not the property.
Beside this you have to use @JsonFormat
@JacksonXmlRootElement
data class User(
var email: String? = "",
var lname: String = "",
var fname: String = "",
@field:JsonFormat(pattern = "yyyy-MM-dd")
@field:JsonSerialize(using = LocalDateSerializer::class)
@field:JsonDeserialize(using = LocalDateDeserializer::class)
var birthday: LocalDate? = null
)
My small test:
val mapper = XmlMapper()
val xml = mapper.writeValueAsString(User(birthday = LocalDate.now()))
println(xml)
Generates the following output:
<User><email></email><lname></lname><fname></fname><birthday>2018-10-19</birthday></User>
How to send current date time to Rest Api with put request in react js?
The problem is that you pass a Date
object so to fix that you need to convert it to json
format like this
new Date().toJSON()
this will return a result like this
console.log(new Date().toJSON())
How to receive and send Date in Spring Rest using JSON?
Found a solution, the problem is the date at Spring is received as "YYYY-MM-ddTHH:mm:ss" so if you want to send it directly into your database you must send in this format or convert it to your database format in the moment you will persist the data.
I do not found an working way to change the format that Spring receives. I just change the format when someone does a GET to receive the information in more friendly format.
For people that like me need date's as ID's for some random reason.
Thanks!
Sending date in a post request from state always returns NULL
In your constructor you are initializing your state twice.
this.state = {
idmedecin: "2",
idpatient: "1",
etat: "1",
date: ""
};
this.state = { chosenDate: new Date() };
When you use this the second time, your initial value for date becomes null instead of empty string because it is overridden. This makes this.state.date = null. What you should do is
this.state = {
idmedecin: "2",
idpatient: "1",
etat: "1",
date: ""
chosenDate: new Date()
};
This will keep the rest of your variables intact. And now instead of null you should see your this.state.date as an empty string.
Related Topics
Java Regex to Validate Full Name Allow Only Spaces and Letters
How to Handle JPA Unique Constraint Violations
Printing a Statement Only Once in a While Loop
How to Create Unique Column With Jpa
Arraylist - How to Check If an Index Exists
How to Create a Spring JPA Repository Findby Query Using a Property That Contains a Keyword
String Match of Only 3 Specific Words
How to Set Environment Variable or System Property in Spring Tests
How to Trim White Space from All Elements in Array
Why Is My Spring @Autowired Field Null
Prevent Multiple Login Using the Same User Name and Password
Regular Expression for Uppercase Letters in a String
Java - How to Write My Arraylist to a File, and Read (Load) That File to the Original Arraylist
How to Find an Object in an Arraylist by Property
@Responsebody , Responseentity Spring Return Object as Json
How to Convert Linkedhashmap to Custom Java Object
How to Sort Date in Descending Order Using Comparator
How to Find Distinct Rows With Field in List Using JPA and Spring