【问题标题】:Save Room Reservation Using AngularJS, Spring and Hibernate使用 AngularJS、Spring 和 Hibernate 保存房间预订
【发布时间】:2016-02-06 20:53:21
【问题描述】:

场景:

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

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('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;
})

最后,这是请求负载,我通过验证器运行了它的内容并检查了它:

{"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 在发布预订时返回的一堆错误。 似乎是开始日期和结束日期已过的问题。

    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

【问题讨论】:

  • 将您的日志汇总到DEBUG,看看映射到底有什么问题。
  • 除非我做错了,否则日志已设置为 DEBUG,但没有显示错误。我在想这个请求可能甚至没有发送到我的 RestController,因为如果它发送了,它应该触发我的 POST 方法的 catch 块并触发 409 Conflict 错误。
  • 对;问题在于数据绑定。如果你打开org.springframework.web,它会描述整个解析和绑定过程。
  • 在我的 application.properties 文件中,我有一个属性:logging.level.org.springframework.web = DEBUG 这就是我需要的,对吗?
  • 除非您有更全面的日志记录配置,否则是的,它将配置基本的引导日志设置。当您发出请求时,您应该会看到来自 DispatcherServlet 的大量消息。

标签: java angularjs spring hibernate spring-mvc


【解决方案1】:

错误信息非常具体:

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")

【讨论】:

  • 这里没有列出其他代码问题,但这是最大的问题。解决这个问题有助于让球滚动。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多