JSON parse error

0

I'm developing a web application (Spring) and need to save a date in the database. I have done a Ajax function to save the value, however 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 Maven to download dependencies to work with JSON. Here 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>

Class model attribute:

@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));

    }

Ajax Script:

 <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 could I solve this problem?

    
asked by anonymous 07.02.2018 / 19:30

1 answer

0

The date you are passing 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 you can help: link

link

    
07.02.2018 / 20:16