如何将日期值发送到Webservice Request中的Calendar对象。
@Produces("application/json")
@Path("Service")
public class ServiceImpl {
@Context
HttpServletRequest httpServletRequest;
@Context
HttpServletResponse httpServletResponse;
@POST
@Path("getDetails")
@Consumes("application/json")
@Produces("application/json")
public String getDetails (ServiceRequest request)
{
if(null!=request)
return "Successfully Parsed given Date";
}
}
public class ServiceRequest {
private XMLGregorianCalendar requestDate;
void setDate(XMLGregorianCalendar date){
this.requestDate = date;
}
XMLGregorianCalendar getDate()
{
return requestDate;
}
}
请求网址: http://localhost:7001/WebProject/Service/getDetails
RequestParams:
{
"requestDate" : "2015-04-20T05:30:00.000Z"
}
它没有将值解析为日期。我怎么能为Calendar对象传递值。
使用genson jar来序列化请求。错误信息就像
Caused by: com.owlike.genson.JsonBindingException: Could not parse date 2015-04-20T05:11:00.000Z
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:1003)
at com.owlike.genson.convert.DefaultConverters$CalendarConverter.deserialize(DefaultConverters.java:1226)
at com.owlike.genson.convert.DefaultConverters$CalendarConverter.deserialize(DefaultConverters.java:1209)
at com.owlike.genson.convert.BeanViewConverter.deserialize(BeanViewConverter.java:102)
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56)
at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:30)
... 48 more
Caused by: java.text.ParseException: Unparseable date: "2015-04-20T05:11:00.000Z"
at java.text.DateFormat.parse(DateFormat.java:337)
at com.owlike.genson.convert.DefaultConverters$DateConverter.read(DefaultConverters.java:1009)
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:1001)
... 53 more
答案 0 :(得分:1)
如果您使用的是Genson 1.2且年龄大于是,那么评论中指出的两个答案应该适合您。 从Genson 1.3开始,围绕配置JAX-RS扩展进行了轻微的重构,所以现在它应该更容易实现。 使用Jersey的一个例子:
Genson genson = new GensonBuilder()
.useDateFormat(yourDateFormat)
.create();
new ResourceConfig().register(new GensonJaxRSFeature().use(genson));
但注册自定义实例的旧方法仍应继续使用1.3及更高版本。
以下是有关jax-rs和configuration的相关文档。