Jquery Ajax call + Spring boot controler = date modification

Viewed 143

I have this Ajax call:

$.ajax({
    type : 'GET',
    headers : {
        Accept : "application/json; charset=utf-8",
        "Content-Type" : "application/json; charset=utf-8"
    },
    url : 'prises/histo',
    success : function(result) {
        google.charts.load('current', {
            'packages' : [ 'corechart', 'table' ]
        });
        google.charts.setOnLoadCallback(function() {
            drawHisto(result);
        });
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.message);
    }
});

Calling this Spring Boot controller:

@RestController
@RequestMapping(value = "/prises")
public class PriseController {
    
    private static final Logger log = LoggerFactory.getLogger(PriseController.class);
    
    @Autowired
    private PriseHistoRepository priseHistoRepository;
            
    @GetMapping(value="histo")
    @ResponseBody
    List<PriseHisto> reportAllHisto(){
        return priseHistoRepository.findAll ();    
    }
}

To get a list of this entity:

public class PriseHisto {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Version
    private Integer version;   

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy/MM/dd", locale = "fr_FR")
    private Date dateDonnees;

    @NotNull
    private String operateurInfra;

    @NotNull
    private String codeOperateurInfra;

    @NotNull
    private String statut;

    private String ocDernierRacc;
    
    @NumberFormat
    @NotNull
    private int nbrPrises;
}

I have a problem with the dateDonnees field: All values are first day of a month (eg 1 dec 2020) in database and in the controler when debugging, but I get last day of the previous month in the result of the AJAX call (eg 30 nov 2020).

JAVA side

Front Side

It looks like a timezone issue during json convertion to javascript object but I have tried many differents setting and can't sort it out. Do you have any idea?

1 Answers

Configuring the default TimeZone in application.properties

spring.jackson.time-zone=Europe/Paris

Or set the Time Zone in JsonFormat

 @JsonFormat(... pattern="yyyy/MM/dd", timezone="Europe/Paris")
Related