I want to pass timestamp as a request parameter in spring boot controller method
//entity class
@Entity
public class EventCalendar {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private int id;
private String eventName;
private String city;
private String address;
@Temporal(TemporalType.TIMESTAMP)
private Date createRecord;
@Temporal(TemporalType.TIMESTAMP)
private Date startTime;
@Temporal(TemporalType.TIMESTAMP)
private Date endTime;
//controller
@RestController
@RequestMapping("/events")
public class controller{
@GetMapping("/getevents8")
public List<EventCalendar> getEvents8(@RequestParam int page,@RequestParam Date d1,@RequestParam Date d2 ){
Sort sort=new Sort(Sort.Direction.DESC,"createRecord");
Pageable pageRequest =PageRequest.of(page, 3,sort);
List<EventCalendar> events;
events=eventRepository.findByCreateRecordBetween(d1, d2, pageRequest);
return events;
}
}
//timestamp example that I inserted using json
{
"id":10,
"eventName":"Kabbadi pro kidz",
"city":"Noida",
"address":"sector 63",
"createRecord":"2020-03-31T15:45:01Z",
"startTime":"2020-04-08T07:30:10Z",
"endTime":"2020-04-09T10:15:18Z",
}
Now what I want to do is pass the time stamp in request Param
I am passing this request in postMan
http://localhost:8082/events/getevents8/?page=0&d1=2020-04-01T09:18:18Z&d2=2020-04-06T23:15:18Z
But getting this error:
eclipse
Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'
Postman
{
"timestamp": "2020-03-31T10:11:34.196+0000",
"status": 400,
"error": "Bad Request",
"message": "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 '2020-04-01T09:18:18Z'; nested exception is java.lang.IllegalArgumentException",
"path": "/events/getevents8/"
}
I Do not know how to pass timestamp without the Json Object I want to know how to pass as a parameter in a request.
You can use this format for your input
2020-04-01T09:18:18Z -> @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
or use iso dateTime input like below
2020-04-01T09:18:18.000+00:00 -> @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
below code work with your input
@GetMapping("/getevents8")
public List<EventCalendar> getEvents8(@RequestParam int page,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") Date d1,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") Date d2) {
Sort sort = new Sort(Sort.Direction.DESC, "createRecord");
Pageable pageRequest = PageRequest.of(page, 3, sort);
List<EventCalendar> events;
events = eventRepository.findByCreateRecordBetween(d1, d2, pageRequest);
return events;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.