gpt4 book ai didi

java - 列表到 Json 数组 InvocationTargetException

转载 作者:行者123 更新时间:2023-11-28 23:59:20 25 4
gpt4 key购买 nike

我有一个包含报价的 util.List。 class offer 的变量是:

Integer id, Date startDate, Date endDate, String offerMessage, String creationDate, String updateDate.

我使用 NetBeans 从 MySQL 数据库生成了 JPA 实体和 Controller ,我在数据库中将 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 网络服务时,我会做类似的事情:

在网络服务中:

reply = _protocol.reply(request);

在协议(protocol)中:

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;
}

我得到异常:java.lang.reflect.InvocationTargetException 消息:java.lang.reflect.InvocationTargetException

我可以想象,问题出在 util.Date 上,我在这里搜索了其他问题,但我没有设法解决这个问题。我应该怎么办?也许将时间戳字段作为整数存储在数据库中,并为数据库转换进行事前操作?

最佳答案

私有(private)静态类 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);
}
}

关于java - 列表到 Json 数组 InvocationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30464231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com