1

I have a util.List with offers. The variables of class offer are:

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

I generated JPA entities and Controller with NetBeans from a MySQL database where i had set startDate, endDate as Timestamp in the db.

As you can see the annotations in the entity are:

@Basic(optional = false)
@Column(name = "startDate")
@Temporal(TemporalType.TIMESTAMP)
private Date startDate;

@Basic(optional = false)
@Column(name = "endDate")
@Temporal(TemporalType.TIMESTAMP)
private Date endDate;

When i test my axis webservice where i do something like:

in WebService:

reply = _protocol.reply(request);

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

I get Exception: java.lang.reflect.InvocationTargetException Message: java.lang.reflect.InvocationTargetException

As i can imagine, the problem is the util.Date, i searched in other questions here, but i did not managed to solve this. What should i do? Maybe store the Timestamp field as integer in db and make pre-post actions for database conversions?

1 Answer 1

1

private static class DateTimeTypeConverter implements 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);
  }
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

and then what should i do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.