我执行RESTful应用程序以接受并保存日期。当我使用以下JSON {“date”:“1997-07-16T19:20:30 + 01:00”,“count”:1}调用它时,我收到下面列出的错误。没有日期它运作良好。我使用jersey.version 2.5.1和eclipselink.version 2.5.2-M1。我该如何解决这个问题?
资源
@POST
@Path(value = "/")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveDate(DateMapper date) {
dateService.save(date);
return Response.status(Response.Status.OK).entity("Date has been successfully saved").type(MediaType.APPLICATION_JSON).build();
}
DateMapper
@XmlRootElement(name = "date")
public class DateMapper {
private Date date;
private int count;
//getters and setters
}
错误
16:36:34,123 SEVERE [com.sun.jersey.spi.container.ContainerResponse] (default task-6) Mapped exception to response: 500 (Internal Server Error): javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error
at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:127) [genson-0.98.jar:]
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:203) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) [jersey-bundle-1.14.jar:1.14]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) [jersey-bundle-1.14.jar:1.14]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:168) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:687) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
Caused by: com.owlike.genson.TransformationException: Could not deserialize to property 'date' of class class au.com.example.mapper.DataMapper
at com.owlike.genson.reflect.PropertyMutator.couldNotDeserialize(PropertyMutator.java:56) [genson-0.98.jar:]
at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:39) [genson-0.98.jar:]
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:116) [genson-0.98.jar:]
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:98) [genson-0.98.jar:]
at com.owlike.genson.convert.BeanViewConverter.deserialize(BeanViewConverter.java:109) [genson-0.98.jar:]
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:61) [genson-0.98.jar:]
at com.owlike.genson.Genson.deserialize(Genson.java:452) [genson-0.98.jar:]
at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:125) [genson-0.98.jar:]
... 42 more
Caused by: com.owlike.genson.TransformationException: Could not parse date 2009-11-13T10:39:35Z
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:824) [genson-0.98.jar:]
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:788) [genson-0.98.jar:]
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:61) [genson-0.98.jar:]
at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:37) [genson-0.98.jar:]
... 48 more
Caused by: java.text.ParseException: Unparseable date: "2009-11-13T10:39:35Z"
at java.text.DateFormat.parse(DateFormat.java:366) [rt.jar:1.8.0_25]
at com.owlike.genson.convert.DefaultConverters$DateConverter.read(DefaultConverters.java:830) [genson-0.98.jar:]
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:822) [genson-0.98.jar:]
... 51 more
答案 0 :(得分:4)
这里的问题是服务器上用于反序列化日期的格式与您在json中使用的格式不匹配。
您需要配置用于Genson的日期格式。 见this answer。另请查看the Genson documentation以了解如何提供自定义Genson实例。
总的来说,您的代码看起来应该是这样的(对于最新的Genson 1.1版):
@Provider
public class GensonProvider implements ContextResolver<Genson> {
private final Genson genson = new GensonBuilder().useDateFormat(yourDateFormat).create();
@Override
public Genson getContext(Class<?> type) {
return genson;
}
}