问题
I do RESTful application to accept and save the date. When I call it with the following JSON {"date": "1997-07-16T19: 20: 30 + 01: 00", "count": 1} I get the error listed below. Without date it works well. I use jersey.version 2.5.1 and eclipselink.version 2.5.2-M1. How do I fix this?
Resource
@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
}
Error
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
回答1:
The problem here is that the format used on the server to deserialize a date does not match the format you used in your json.
You need to configure the date format to use for Genson. See this answer. Also have a look at the Genson documentation to see how to provide a custom Genson instance.
Overall your code should look something like that (for latest Genson release 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;
}
}
来源:https://stackoverflow.com/questions/26451873/could-not-parse-date-in-restful-application-with-jersey