0

When I tried to get with these url : http://www.localhost:8080/api/employee/search?startDate=2000-10-22&salary=10000

This error message shows up : "Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value '2000-10-22'; nested exception is java.lang.IllegalArgumentException"

Params : startDate = 2000-10-22 salary = 10000

What is the problem here ?

Employee Class :

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotBlank
    private String nationalId;

    @NotBlank
    private String name;

    @NotBlank
    private String surname;

    private Integer salary;

    @JoinColumn(name="start_date")
    @JsonFormat(pattern="yyyy-MM-dd")
    private Date startDate;

    private String office;

    private String department;

Query in my repository :

  @Query("Select e FROM Employee e  " +
            "where " +
            " (:startDate is NULL or e.startDate > :startDate) " +
            " AND " +
            " (:salary is NULL or e.salary > :salary) ")
    List<Employee> searchEmployees(Date startDate,Integer salary);

ServiceImpl:

 @Override
    public List<Employee> searchEmployees(Date startDate,Integer salary){
        List <Employee> employees = employeeRepository.searchEmployees(startDate,salary);
        return employees;
    }

Controller :

@GetMapping(path="/search")
    public ResponseEntity<List<Employee>> searchEmployees(@RequestParam("startDate") Date startDate,
                                                          @RequestParam("salary") Integer salary){
        return ResponseEntity.ok(employeeService.searchEmployees(startDate,salary));
    }
2
  • The link you provided is a localhost link. So I hope you understand that it can't be seen by anyone. Commented Jun 22, 2022 at 17:09
  • Yes,that is the url that after after I put the params on postman. I wrote that for if I made a mistake there Commented Jun 22, 2022 at 20:29

2 Answers 2

3

This is because by default, Spring cannot convert String parameters to any date or time object.

Use this instead.

@RequestParam @DateTimeFormat(pattern= "yyyy-MM-dd") Date startDate

Sign up to request clarification or add additional context in comments.

Comments

0

problem is java convert string to date you can try this

@GetMapping(path="/search")
public ResponseEntity<List<Employee>> searchEmployees(@RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
                                                      @RequestParam("salary") Integer salary){
    return ResponseEntity.ok(employeeService.searchEmployees(startDate,salary));
}

1 Comment

With that I get this error "org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.