I am working on 2 Spring Boot projects that comunicate between them (the first project call a POST API defined on the second project and send to it a DTO object in JSON format. Both the projects at the moment runs on the same machine so they should have the same timezone (I suppose...)
I have a problem sending correctly a Date format. I will try to explain what I am doing and what problem I am facing.
In the first project I have this DTO object:
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonPropertyOrder(alphabetic=true)
public class OneRowReadTrain1DTO {
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = JsonFormat.DEFAULT_TIMEZONE)
@JsonProperty("Time_Stamp")
private Date timeStamp;
private Double v1;
private Double v2;
........................................
........................................
........................................
CONSTRUCTOR AND GETTER AND SETTER METHODS
}
As you can see I have this timeStamp field that is annotated with this annotation to convert the field in JSON:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = JsonFormat.DEFAULT_TIMEZONE)
it should also set the default timezone
Then into a class of this first project I have this method that perform the POST request to the API defined in the second project:
@Scheduled(fixedRate=90000)
public void insertOneRowReadTrain1Job() {
System.out.println("COUNTER: " + counter);
OneRowReadTrain1DTO currentElement = excelRowAsDtoList.get(counter);
System.out.println("CURRENT DTO: " + currentElement);
String insertApiUrl = baseUrl + "oneRowReadTrain1/";
try {
uri = new URI(insertApiUrl);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResponseEntity<Integer> result = restTemplate.postForEntity(uri, currentElement, Integer.class);
System.out.println("result: " + result);
System.out.println("--------------------------------------------------------------------------------------------");
counter++;
}
As you can see I am performing the API call by this line:
ResponseEntity<Integer> result = restTemplate.postForEntity(uri, currentElement, Integer.class);
Using the debugger the timeStamp field value seems to be correct, infact this field value is:
OneRowReadTrain1DTO [timeStamp=Sat Oct 17 06:00:14 PDT 2009, v1=6.5718506, v2=538.47812, ......]
As you can see the value of this Date field is timeStamp=Sat Oct 17 06:00:14 and it is the expected value.
Then the API defined in the second project is called and here I am obtaining a strange behavior with this field value.
In the second project the called API is:
@PostMapping(value = "/train/{trainName}", consumes = "application/json")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public int insertTrend(@PathVariable("trainName") String trainName,
@RequestBody Map<String, Object> jsonTrainInfo) throws IOException {
int result = 0;
System.out.println("trainName: " + trainName);
System.out.println("JSON: " + jsonTrainInfo);
result = trainService.insertOneRowReadTrain(trainName, jsonTrainInfo);
return result;
}
As you can see the obtained payload is into this method parameter:
@RequestBody Map<String, Object> jsonTrainInfo
The problem is that I am obtaining something like this:
{Time_Stamp=2009-10-17 13:00:14, v1=6.5718506,.....}
As you can see the value of this field is: Time_Stamp=2009-10-17 13:00:14 where the date section (2009-10-17) is correct but the time section is totally wrong, infact the obtained time section is 13:00:14 and not the expected 06:00:14 (the one that is present into the sent object).
Now from what I know 6:00 PDT equals 13:00 GMT but why have I this problem? I need that the received date is in the same time zone (or am I missing something?)
Why when it is received into the Map<String, Object> jsonTrainInfo the timezone seems to be changed?
What is wrong? What am I missing? How can I try to fix this issue? I am going crazy