java.sql.TimeStamp没有默认的构造函数

时间:2019-01-26 10:21:22

标签: java json jersey-2.0

我正在用Java编写Web服务,但有一件事情我坚持。我想将java.sql.TimeStamp发送为json响应,但收到了 IllegalAnnotationException 。我正在使用 jersey 1.19 ,以下是例外。

javax.ws.rs.WebApplicationException:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.sql.Timestamp does not have a no-arg default constructor.
this problem is related to the following location:
    at java.sql.Timestamp
    at public java.sql.Timestamp response.TransactionStatusResponse.command_receiving_datetime
    at response.TransactionStatusResponse
    at public java.util.List response.OnDemandRequestResponse.getData()
    at response.OnDemandRequestResponse

以下显示的是我的 JsonResponseModel 类。

@XmlRootElement
public class TransactionStatusResponse {

     public TransactionStatusResponse() {
     }  

     public String transaction_id;
     public String msn;
     public String global_device_id;
     public String type;
     public String type_parameters;
     public Timestamp command_receiving_datetime;

}

我也看过java.sql.TimeStamp类,它没有默认的构造函数。因此,有什么方法可以在json响应中不发送default-arg构造函数对象。

1 个答案:

答案 0 :(得分:1)

我认为您需要为此注册适配器

public class TimestampAdapter extends XmlAdapter<Date, Timestamp> {
      public Date marshal(Timestamp v) {
          return new Date(v.getTime());
      }
      public Timestamp unmarshal(Date v) {
          return new Timestamp(v.getTime());
      }
  }

然后像这样注释您的时间戳

@XmlJavaTypeAdapter( TimestampAdapter.class)
        public Timestamp done_date;

,您应该使用java.util.Date而不是java.sql.Date