简体   繁体   English

Java-Json + Jersey RESTful中使用的日期值

[英]Java - Date values used in Json + Jersey RESTful

I have a java simple web-service which receives a String and a Date value. 我有一个Java简单的Web服务,它接收一个String和一个Date值。 The thing is that if I make it receive the String only, it works, but with the Date field then it throws an exception saying "Error 500". 关键是,如果我仅接收字符串,则可以工作,但是使用日期字段,则抛出异常,提示“错误500”。

Here's the Entity and the Web-Serice in java: 这是java中的Entity和Web服务:

@Entity
@XmlRootElement
public class entityABC implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    private String userId;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date startTime;
}

The service declaration: 服务声明:

@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(EntityABC entity) {
    super.create(entity);
}

Now, I send this Json (POST) from a C# app using JavaScriptSerializer() : 现在,我使用JavaScriptSerializer()从C#应用程序发送此Json(POST JavaScriptSerializer()

{\"userId\":\"mail@gmail.com\",\"startTime\":\"\\/Date(1380333600000)\\/\"}

What am I doing wrong here? 我在这里做错了什么? This happens only with the Date fields; 仅在“ Date字段中会发生这种情况。 with the other basic field types it works OK. 与其他基本字段类型一起正常工作。

UPDATE: 更新:

[2013-09-28T03:14:46.383-0430] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=97 _ThreadName=http-listener-1(4)] [timeMillis: 1380354286383] [levelValue: 900] [[
  StandardWrapperValve[outlook.crm.service.ApplicationConfig]: Servlet.service() for servlet outlook.crm.service.ApplicationConfig threw exception
java.lang.IllegalArgumentException: Object: null is not a known entity type.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:493)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287)
    at outlook.crm.service.AbstractFacade.create(AbstractFacade.java:22)
    at outlook.crm.service.AppointmentFacadeREST.create(AppointmentFacadeREST.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at $Proxy321.create(Unknown Source)
    at outlook.crm.service.__EJB31_Generated__AppointmentFacadeREST__Intf____Bean__.create(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:125)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$VoidOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:136)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:91)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:346)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:341)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:224)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:722)
]]

You mentioned that you are using Jersey, but you don't say which binding provider you are using. 您提到您正在使用Jersey,但是没有说您正在使用哪个绑定提供程序 Regardless of what binding provider you are using, you'll need a way of telling your binding provider how it should parse dates. 无论使用哪种绑定提供程序,都需要一种方法来告诉绑定提供程序应如何解析日期。 I think it should be possible to configure a default date parser globally, but it's probably easier to add an extra annotation to your POJO that defines the date format. 我认为应该可以在全局范围内配置默认的日期解析器,但是向POJO添加定义日期格式的附加注释可能更容易。

If you are using Moxy, you could configure a custom date parser using @XmlJavaTypeAdapter as demonstrated here . 如果您使用的莫西,您可以配置使用自定义的日期解析器@XmlJavaTypeAdapter作为证明这里 Similar mechanisms exist for other JSON binding providers. 其他JSON绑定提供程序也存在类似的机制。 Moxy has the advantage that it uses the same annotations to customize both the XML & JSON bindings. Moxy的优势在于,它使用相同的批注来自定义XML和JSON绑定。

If you are using a standard date representation (eg ISO-8601 ) , the binding provider might even support it out of the box so that you don't need to write a custom date parser (although it isn't difficult to do). 如果您使用的是标准日期表示法(例如ISO-8601 ),则绑定提供程序甚至可以开箱即用地支持它,因此您无需编写自定义日期解析器(尽管并不困难)。

In the json snippet you provided the date is represented as "startTime":"\\/Date(1380333600000)\\/" with 1380333600000 being the number of milliseconds elapsed since midnight 01 January, 1970 UTC. 在您提供的json片段中,日期表示为"startTime":"\\/Date(1380333600000)\\/"其中1380333600000是自UTC 1970年1月1日午夜以来经过的毫秒数。 In order to parse this you probably need to provide a custom date parser as this isn't a "standard" date representation. 为了对此进行解析,您可能需要提供一个自定义的日期解析器,因为这不是“标准”日期表示形式。

I recommend using a more universal date representation (or at least a more human-readable one) as the contract of your web service should be as universal as possible and not tight to a representation used by a specific framework (in this case the C#'s JavaScriptSerializer ) 我建议使用更通用的日期表示形式(或至少是更易于理解的日期表示形式),因为您的Web服务合同应尽可能通用,并且与特定框架使用的表示形式(在这种情况下为C#的JavaScriptSerializer不紧密)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM