java - 使用 AngularJS、Spring 和 Hibernate 保存房间预订

标签 java angularjs spring hibernate spring-mvc

场景:

预订由用户预订特定房间。预订实体具有以下属性:

private Long reservationId;
private Date startTime;
private Date endTime;
private User user;
private Room room;
private String note;

正如您所看到的,预订实体也接受用户和房间实体。 我正在尝试将此预订保存到数据库中,但在保存时不断遇到以下问题:

HTTP Status 400 -description The request sent by the client was syntactically incorrect.

我认为我的问题与错误地传递 User 和 Room 对象有关,但我还没有完全弄清楚问题所在。

这是我的 Spring @RestController 方法:

@RequestMapping(method=RequestMethod.POST)
public ResponseEntity<ReservationResource> addReservation(@RequestBody ReservationResource reservation){
    try{
        Reservation newReservation = reservationService.addReservation(reservation.toReservation());
        ReservationResource reservationResource = new ReservationResourceAsm().toResource(newReservation);

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(URI.create(reservationResource.getLink("self").getHref()));
        return new ResponseEntity<ReservationResource>(reservationResource, headers, HttpStatus.CREATED);
    } catch (ReservationExistsException exception) {
        throw new ConflictException(exception); 
    }
}

这是我的 Angular Controller :

.controller('AddReservationController', function($scope, $state, $http, reservationService){
    $scope.reservation = {};

    $http.get("/libroomreserve/api/user/1").then(
        function(resource){
            console.log(resource);
            $scope.reservation.user = resource.data;
        },
        function(){
            $scope.reservation.user = null;
        }
    );
    $http.get("/libroomreserve/api/room/1").then(
        function(resource){
            $scope.reservation.room = resource.data;
        },
        function(){
            $scope.reservation.room = null;
        }
    );

    $scope.newReservation = function(){
    reservationService.addReservation(
        $scope.reservation,
        function(data){
            console.log("Success! Data printing:");
            console.log(data);
            $state.go("home");
        },
        function(data){
            console.log("Failure! Data printing:");
            console.log(data);
        }
    );
};
});

以及相应的 Angular 工厂服务:

.factory('reservationService', function($resource){
var reservations = {};

reservations.addReservation = function(reservation, success, failure){
    var Reservation = $resource('/libroomreserve/api/reservation');
    Reservation.save({}, reservation, success, failure);
};

return reservations;
})

最后,这是请求有效负载,我通过 validator 运行了其内容并进行了检查:

{"user":{"userId":1,"userName":"tom","links":[{"rel":"self","href":"http://localhost:8080/libroomreserve/api/user/1"}]},"room":{"roomId":1,"roomNumber":"101A","roomDescription":"Best room ever","roomCapacity":15,"links":[{"rel":"self","href":"http://localhost:8080/libroomreserve/api/room/1"}]},"startTime":"2015-12-25 00:00:00","endTime":"2015-12-25 00:00:00","note":"dsfds"}

更新

这是 log4j 在 POST 预订时返回的一堆错误。 似乎是开始日期和结束日期已过的问题。

    DispatcherServlet with name 'dispatcher' processing POST request for [/libroomreserve/api/reservation]
Looking up handler method for path /api/reservation
Returning handler method [public org.springframework.http.ResponseEntity<com.ucrisko.libroomreserve.rest.resources.ReservationResource> com.ucrisko.libroomreserve.rest.controllers.ReservationController.addReservation(com.ucrisko.libroomreserve.rest.resources.ReservationResource)]
Returning cached instance of singleton bean 'reservationController'
Skip CORS processing, request is a same-origin one
Read [class com.ucrisko.libroomreserve.rest.resources.ReservationResource] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@ec39299]
Error resolving argument [0] [type=com.ucrisko.libroomreserve.rest.resources.ReservationResource]
HandlerMethod details: 
Controller [com.ucrisko.libroomreserve.rest.controllers.ReservationController]
Method [public org.springframework.http.ResponseEntity<com.ucrisko.libroomreserve.rest.resources.ReservationResource> com.ucrisko.libroomreserve.rest.controllers.ReservationController.addReservation(com.ucrisko.libroomreserve.rest.resources.ReservationResource)]

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:224)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:208)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:193)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:148)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:125)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:399)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:162)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"])
    at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55)
    at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:904)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:787)
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:175)
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:261)
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:245)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:337)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:131)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3731)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2808)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:221)
    ... 76 more
Resolving exception from handler [public org.springframework.http.ResponseEntity<com.ucrisko.libroomreserve.rest.resources.ReservationResource> com.ucrisko.libroomreserve.rest.controllers.ReservationController.addReservation(com.ucrisko.libroomreserve.rest.resources.ReservationResource)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"])
Resolving exception from handler [public org.springframework.http.ResponseEntity<com.ucrisko.libroomreserve.rest.resources.ReservationResource> com.ucrisko.libroomreserve.rest.controllers.ReservationController.addReservation(com.ucrisko.libroomreserve.rest.resources.ReservationResource)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"])
Resolving exception from handler [public org.springframework.http.ResponseEntity<com.ucrisko.libroomreserve.rest.resources.ReservationResource> com.ucrisko.libroomreserve.rest.controllers.ReservationController.addReservation(com.ucrisko.libroomreserve.rest.resources.ReservationResource)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"])
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"])
SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
Handler execution resulted in exception: Could not read document: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation (error: Failed to parse Date value '2015-12-25 00:00:00': Can not parse date "2015-12-25 00:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.PushbackInputStream@1f05a63c; line: 1, column: 295] (through reference chain: com.ucrisko.libroomreserve.rest.resources.ReservationResource["startTime"])
Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
Successfully completed request
Chain processed normally
SecurityContextHolder now cleared, as request processing completed

最佳答案

错误消息非常具体:

Can not construct instance of java.util.Date from String value '2015-12-25 00:00:00': not a valid representation

默认情况下,Jackson 尝试将日期映射到 ISO-8601:

standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

您需要对相关属性进行注释,以告诉 Jackson 使用什么格式。像这样的东西应该有效:

@JsonFormat(shape=Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")

关于java - 使用 AngularJS、Spring 和 Hibernate 保存房间预订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33554430/

相关文章:

java - 如何使用java检查正在外部更新的文件的内容?

java - wait(n) 每次我改变同步关键字的位置时都会表现不同

java - 如何通过避免重复来缩短 Tic Tac Toe 代码

java - smartsheet cell.getFormat() 返回 null

javascript - 非异步路径返回什么?

$http.get 上的 Javascript 回调

java - Spring Boot,使用 EhCache 进行缓存

java - 未生成查询 DSL Q 类型类

angularjs - 从 OPENSHIFT_DATA_DIR 提供图像的缓存问题

java - Java + Kotlin SpringBoot 项目编译失败