Java Json数组调用TargetException的列表
我有一个包含报价的util.List。课程提供的变量包括:Java Json数组调用TargetException的列表,java,mysql,json,jpa,Java,Mysql,Json,Jpa,我有一个包含报价的util.List。课程提供的变量包括: Integer id, Date startDate, Date endDate, String offerMessage, String creationDate, String updateDate. 我使用NetBeans从MySQL数据库生成了JPA实体和控制器,我在数据库中将startDate、endDate设置为时间戳 如您所见,实体中的注释包括: @Basic(optional = false) @Column(name
Integer id, Date startDate, Date endDate, String offerMessage, String creationDate, String updateDate.
我使用NetBeans从MySQL数据库生成了JPA实体和控制器,我在数据库中将startDate、endDate设置为时间戳
如您所见,实体中的注释包括:
@Basic(optional = false)
@Column(name = "startDate")
@Temporal(TemporalType.TIMESTAMP)
private Date startDate;
@Basic(optional = false)
@Column(name = "endDate")
@Temporal(TemporalType.TIMESTAMP)
private Date endDate;
当我测试axis Web服务时,我会执行以下操作:
在WebService中:
reply = _protocol.reply(request);
在协议中:
public String reply(String request) throws Exception {
String reply = "I dont understand you!";
try {
if (request.equals("a")) {
OfferJpaController oferJpaController = new OfferJpaController();
List<Offer> allOffers = oferJpaController.findOfferEntities();
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateTypeAdapter() ).create();
reply = gson.toJson(allOffers);
}
} catch (Exception ex) {
throw ex;
}
return reply;
}
公共字符串回复(字符串请求)引发异常{
String reply=“我不明白你的意思!”;
试一试{
如果(请求等于(“a”)){
oferJpaController的OfferJpaController=新的OfferJpaController();
List allOffers=oferjpaccontroller.findofferenties();
Gson Gson=new GsonBuilder().registerTypeAdapter(Date.class,new DateTypeAdapter()).create();
reply=gson.toJson(allOffers);
}
}捕获(例外情况除外){
掷骰子;
}
回复;
}
我得到异常:java.lang.reflect.InvocationTargetException消息:java.lang.reflect.InvocationTargetException
正如我所能想象的,问题是util.Date,我在这里搜索了其他问题,但我没有设法解决这个问题。我该怎么办?可能将时间戳字段存储为数据库中的整数,并为数据库转换执行预post操作?私有静态类DateTimeTypeConverter 实现JsonSerializer、JsonDeserializer{
@Override
public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
@Override
public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context)
throws JsonParseException {
try {
return new DateTime(json.getAsString());
} catch (IllegalArgumentException e) {
// May be it came in formatted as a java.util.Date, so try that
Date date = context.deserialize(json, Date.class);
return new DateTime(date);
}
}
}然后我该怎么办?