Hola a todos yo estoy teniendo el mismo problema, e intentado todo espero me puedan ayudar. Desde ya muchas gracias.
Model
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
@Entity
@Table(name = "personas")
@Data
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 60)
private String nombre;
@Column(nullable = false, length = 60)
private String apellido;
@Column(nullable = false, unique = true, length = 10)
private String dni;
@JsonFormat(pattern="yyyy-MM-dd")
@Column(name = "fecha_alta")
private LocalDateTime fechaAlta;
@JsonFormat(pattern="yyyy-MM-dd")
@Column(name = "fecha_modificacion")
private LocalDateTime fechaModificacion;
@PrePersist
private void antesDePersistir(){
this.fechaAlta = LocalDateTime.now();
}
@PreUpdate
private void antesDeUpdate(){
this.fechaModificacion = LocalDateTime.now();
}
}
Controller
@GetMapping("/persons/get/")
public List<Person> getByStartDateBetween(@RequestParam(name = "from") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime from,
@RequestParam (name = "to") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime to) {
return repository.findByFechaAltaBetween(from, to);
Repository
@Modifying
@Transactional
@Query("select p from Person p where p.fechaAlta >= :from and p.fechaAlta <= :to ")
List<Person> findByFechaAltaBetween(@Param("from") LocalDateTime from, @Param("to") LocalDateTime to);
Postman ----------------------------------------------------------------------------
Error
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2022-02-01'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2022-02-01]", "path": "/api/persons/get/"