0
I am developing a web application (Spring) and need to save a date in the database. I made a function Ajax
to save the value, but I get the following error:
JSON parse error: Can not deserialize value of type java.time.Localdate from String "02/07/2018": Text '02/07/2018' could not be Parsed at index 0
I’ve already set up the Maven
to download the dependencies to work with JSON. Below are the dependencies I’m using:
<!-- Jackson - JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Attribute of class model:
@Column(name = "data_contribuicao", columnDefinition = "DATE")
@NotBlank(message = "Informe a data da contribuição")
private LocalDate dataContribuicao;
Controller:
@PostMapping("/salvar")
private @ResponseBody ResponseEntity<?> salvar(@RequestBody @Validated Contribuicao contribuicao, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return ResponseEntity.badRequest().body(bindingResult.getFieldError().getDefaultMessage());
}
return ResponseEntity.ok(cadastroContribuicaoServe.cadastrar(contribuicao));
}
Script Ajax:
<script>
$(document).ready(function() {
$("#enviar").click(function() {
var contribuicao = {
"id" : $('#id').val(),
"valor" :$('#valor').val(),
"dataContribuicao" : $('#dataContribuicao').val()
}
$.ajax({
type: "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url: "salvar",
data: JSON.stringify(contribuicao),
success :function(result) {
alert("Salvo com sucesso!") //alterar resposta
}
});
});
});
</script>
I tried several alternatives but without success. How I could solve this problem?
Thank you for the answer! I take this opportunity to say that now I have another error, rs: "JSON parse error: Can not deserialize value of type java.lang.Double from String "15.00": not a Valid Double value;"
– Victor Alves
What mistake? Put it there
– LR10
This: "JSON parse error: Can not deserialize value of type java.lang.Double from String "15.00": not a Valid Double value;" I have another attribute in the form, and I also need to save it. Attribute of class model:
@NotBlank(message = "Por favor, informe o valor da contribuicao")
 @Column(name = "valor_contribuicao")
 private Double contribuicao;
– Victor Alves
and a conversion error..... vc is passing a string to a Double.
– LR10
Yes, but how can I solve this? I don’t have so much knowledge in JS. I need to take the form values and pass the correct type to the controller object.
– Victor Alves
as you are passing so valore , passes without the commas. 15,00 to 15
– LR10
@Victoralves use Bigdecimal
– Dherik