gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:41:52 26 4
gpt4 key购买 nike

我正在用 Java 编写 Web 服务,但有一件事让我陷入困境。我想将 java.sql.TimeStamp 作为 json 响应发送,但收到 IllegalAnnotationException。我使用的是jersey 1.19,下面是异常(exception)。

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 响应中发送无默认参数的构造函数对象。

最佳答案

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

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

关于java.sql.TimeStamp 没有默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54377461/

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