Home > Software design >  Parameter value [2019-05-15,23:00] did not match expected type [java.time.LocalDateTime (n/a)]
Parameter value [2019-05-15,23:00] did not match expected type [java.time.LocalDateTime (n/a)]

Time:06-30

firstly I'm a beginner in Java. I want to ask you, I try to GET request, and I catch an exception.

GET http://localhost:8080/api/criteria/byParam?name=dateTimeAppointment&value=2019-05-15,23:00

java.lang.IllegalArgumentException: Parameter value [2019-05-15,23:00] did not match expected type [java.time.LocalDateTime (n/a)]

what's wrong?

@JsonFormat(pattern="yyyy-MM-dd,HH:mm")
    @Column(name = "local_date_time", columnDefinition = "TIMESTAMP")
    private LocalDateTime dateTimeAppointment;

Controller

@RestController
@AllArgsConstructor
@RequestMapping("/api/criteria")
public class ClientController {
    CriteriaService criteriaService;

    @GetMapping("/byParam")
    public List<Client> getClientsByParam(@RequestParam(value = "name") String name, @RequestParam(value = "value")
            String value) {
        return criteriaService.getClientByParam(name, value);
    }

CriteriaService

public class CriteriaService {
    @PersistenceContext
    EntityManager entityManager;
    public List<Client> getClientByParam(String name, String value) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Client> criteriaQuery = criteriaBuilder.createQuery(Client.class);
        Root<Client> root = criteriaQuery.from(Client.class);
        Predicate predicate = criteriaBuilder.equal(root.get(name), value);

        criteriaQuery.where(predicate);

        return entityManager.createQuery(criteriaQuery).getResultList();
    }

Please, help me how fix it.

CodePudding user response:

Try something like this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd,HH:mm");
Predicate predicate = criteriaBuilder.equal(root.get(name),LocalDateTime.parse(value , formatter));
  • Related