JSON parse error

Asked

Viewed 2,275 times

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?

1 answer

1


The date you are going through is not a local date and date format. Change to specify the correct formatter.

@Column(name = "data_contribuicao", columnDefinition = "DATE")
@NotBlank(message = "Informe a data da contribuição")
@JsonFormat(pattern = "dd/MM/aaaa")
@DateTimeFormat(iso = DateTimeFormatter.ISO_LOCAL_DATE_TIME)
private LocalDate dataContribuicao;  

Here can help you: https://stackoverflow.com/questions/40327970/deserialize-java-8-localdatetime-with-jacksonmapper/40476708#40476708

https://stackoverflow.com/questions/40327970/deserialize-java-8-localdatetime-with-jacksonmapper

  • 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;"

  • What mistake? Put it there

  • 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")&#xA; @Column(name = "valor_contribuicao")&#xA; private Double contribuicao;

  • and a conversion error..... vc is passing a string to a Double.

  • 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.

  • as you are passing so valore , passes without the commas. 15,00 to 15

  • @Victoralves use Bigdecimal

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.