如何在对Spring Boot Controller的GET请求中接受LocalDateTime参数?

时间:2017-05-04 10:59:21

标签: java spring

此问题与this SO problem非常相似,适用于较旧的Date API。

我想用Java 8 LocalDateTime API实现相同的功能。当我这样做时,

@RequestMapping("/locationSnapshot/{userId}/{pointInTime}")
public MyResponse getLocationInTime(
        @PathParam(value="userId") Long userId,
        @PathParam(value="pointInTime")
        @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss") LocalDateTime pointInTime) {

    MyResponse response = new MyResponse();
    return response;
}

我明白了,

Failed to instantiate [java.time.LocalDateTime]: No default constructor
found; nested exception is java.lang.NoSuchMethodException: 
java.time.LocalDateTime.<init>()

2 个答案:

答案 0 :(得分:4)

使用@PathVariable代替@PathParam

@RequestMapping("/locationSnapshot/{userId}/{pointInTime}")
public MyResponse getLocationInTime(
        @PathVariable(value="userId") Long userId,
        @PathVariable(value="pointInTime")
        @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss") LocalDateTime pointInTime) {

    MyResponse response = new MyResponse();
    return response;
}

答案 1 :(得分:0)

尝试在- (void)connectionFailed:(NSString *)title { NSString *message = nil; if ([title isEqualToString:NSLocalizedString(@"Connection Refused", @"connection Refused")]) { message = NSLocalizedString(@"You do not have permission to use this console", @"incorrect permissions message"); } else { message = NSLocalizedString(@"Connection Failed", @"connection failed error message"); } dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil]; }); 参数之前添加@RequestParam