【问题标题】:How to pass Timestamp,date in request param in spring boot using Postman如何使用 Postman 在 Spring Boot 中的请求参数中传递时间戳、日期
【发布时间】:2020-07-11 20:47:07
【问题描述】:

我想在 Spring Boot 控制器方法中将时间戳作为请求参数传递

//实体类

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

//控制器

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

//我使用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",
}

现在我要做的是在请求参数中传递时间戳

我在 postMan 中传递这个请求

http://localhost:8082/events/getevents8/?page=0&d1=2020-04-01T09:18:18Z&d2=2020-04-06T23:15:18Z

但是得到这个错误:

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/"
}

我不知道如何在没有 Json 对象的情况下传递时间戳我想知道如何在请求中作为参数传递。

【问题讨论】:

  • 你在 Postman 中的哪里插入了 Json?
  • 当我在 Json 对象中传递时间戳时,我将时间戳保存在我的实体中,但我想通过请求参数来完成
  • 我认为这可以帮助你:baeldung.com/spring-date-parameters

标签: java spring spring-boot spring-mvc spring-data-jpa


【解决方案1】:

您可以使用这种格式进行输入

    2020-04-01T09:18:18Z -> @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") 

或使用 iso dateTime 输入,如下所示

2020-04-01T09:18:18.000+00:00 -> @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)

下面的代码使用您的输入

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

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2022-11-28
    • 2022-11-28
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多