[英]Can not convert Subclass object to request body json in Retrofit 2.0 with GsonConverterFactory
[英]why i can not convert JSON (GSON) date format to java object from retrofit request body?
我正在將請求正文從 Android(改造)發送到服務器(由 JAVA/JERSEY/JACKSON 支持)。 我正在通過攔截器記錄請求正文,它看起來像什么
{"workId":"456655","workName":"some work","workOrderDate":"Jan 29, 2020 12:00:00 AM"}
在服務器端,我的Project
對象能夠解析除"workOrderDate"
之外的每個字段。 我嘗試使用更改各種類型的 Gson 日期格式
Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd") .create();
但仍然是 JSON,格式沒有改變(2020 年 1 月 29 日 12:00:00 AM)並且它沒有分配給服務器端Project
對象的"workOrderDate"
字段。
服務器端的對象-
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "project_id")
private Integer projectId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 200)
@Column(name = "work_name")
private String workName;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 2147483647)
@Column(name = "work_description")
private String workDescription;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "consignee_work_order")
private String consigneeWorkOrder;
@Basic(optional = false)
@NotNull
@Column(name = "work_order_date")
@Temporal(TemporalType.DATE)
private Date workOrderDate;
@Size(max = 45)
@Column(name = "consignee")
private String consignee;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "sent_by")
private String sentBy;
@Size(max = 45)
@Column(name = "chargeable_head")
private String chargeableHead;
@Size(max = 45)
@Column(name = "account_unit")
private String accountUnit;
@Size(max = 45)
@Column(name = "fund_exist")
private String fundExist;
@Size(max = 45)
@Column(name = "fund_certificate")
private String fundCertificate;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "approx_cost")
private Double approxCost;
@Column(name = "approx_weight")
private Double approxWeight;
@Size(max = 200)
@Column(name = "drawing")
private String drawing;
@Size(max = 45)
@Column(name = "required_at")
private String requiredAt;
@Lob
@Size(max = 2147483647)
@Column(name = "remark1")
private String remark1;
@Lob
@Size(max = 2147483647)
@Column(name = "remark2")
private String remark2;
@Lob
@Size(max = 2147483647)
@Column(name = "remark3")
private String remark3;
@JoinColumn(name = "division", referencedColumnName = "division_id")
@ManyToOne(optional = false)
private Division division;
public Project() {
}
public Project(Integer projectId) {
this.projectId = projectId;
}
public Project(Integer projectId, String workName, String workDescription, String consigneeWorkOrder, Date workOrderDate, String sentBy) {
this.projectId = projectId;
this.workName = workName;
this.workDescription = workDescription;
this.consigneeWorkOrder = consigneeWorkOrder;
this.workOrderDate = workOrderDate;
this.sentBy = sentBy;
}
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public String getWorkName() {
return workName;
}
public void setWorkName(String workName) {
this.workName = workName;
}
public String getWorkDescription() {
return workDescription;
}
public void setWorkDescription(String workDescription) {
this.workDescription = workDescription;
}
public String getConsigneeWorkOrder() {
return consigneeWorkOrder;
}
public void setConsigneeWorkOrder(String consigneeWorkOrder) {
this.consigneeWorkOrder = consigneeWorkOrder;
}
public Date getWorkOrderDate() {
return workOrderDate;
}
public void setWorkOrderDate(Date workOrderDate) {
this.workOrderDate = workOrderDate;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getSentBy() {
return sentBy;
}
public void setSentBy(String sentBy) {
this.sentBy = sentBy;
}
public String getChargeableHead() {
return chargeableHead;
}
public void setChargeableHead(String chargeableHead) {
this.chargeableHead = chargeableHead;
}
public String getAccountUnit() {
return accountUnit;
}
public void setAccountUnit(String accountUnit) {
this.accountUnit = accountUnit;
}
public String getFundExist() {
return fundExist;
}
public void setFundExist(String fundExist) {
this.fundExist = fundExist;
}
public String getFundCertificate() {
return fundCertificate;
}
public void setFundCertificate(String fundCertificate) {
this.fundCertificate = fundCertificate;
}
public Double getApproxCost() {
return approxCost;
}
public void setApproxCost(Double approxCost) {
this.approxCost = approxCost;
}
public Double getApproxWeight() {
return approxWeight;
}
public void setApproxWeight(Double approxWeight) {
this.approxWeight = approxWeight;
}
public String getDrawing() {
return drawing;
}
public void setDrawing(String drawing) {
this.drawing = drawing;
}
public String getRequiredAt() {
return requiredAt;
}
public void setRequiredAt(String requiredAt) {
this.requiredAt = requiredAt;
}
public String getRemark1() {
return remark1;
}
public void setRemark1(String remark1) {
this.remark1 = remark1;
}
public String getRemark2() {
return remark2;
}
public void setRemark2(String remark2) {
this.remark2 = remark2;
}
public String getRemark3() {
return remark3;
}
public void setRemark3(String remark3) {
this.remark3 = remark3;
}
public Division getDivision() {
return division;
}
public void setDivision(Division division) {
this.division = division;
}
@Override
public int hashCode() {
int hash = 0;
hash += (projectId != null ? projectId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Project)) {
return false;
}
Project other = (Project) object;
if ((this.projectId == null && other.projectId != null) || (this.projectId != null && !this.projectId.equals(other.projectId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.nrbwlko.officeprojectmaven1.Project[ projectId=" + projectId + " ]";
}
}
安卓端
public class PojoProject {
private Integer projectId;
private String workName;
private String workDescription;
private String consigneeWorkOrder;
private Date workOrderDate;
private String consignee;
private String sentBy;
private String chargeableHead;
private String accountUnit;
private String fundExist;
private String fundCertificate;
private Double approxCost;
private Double approxWeight;
private String drawing;
private String requiredAt;
private String remark1;
private String remark2;
private String remark3;
private PojoDivision division;
public PojoProject(String workName, String workDescription, String consigneeWorkOrder, Date workOrderDate, String consignee, String sentBy, String chargeableHead, String accountUnit, PojoDivision division) {
this.workName = workName;
this.workDescription = workDescription;
this.consigneeWorkOrder = consigneeWorkOrder;
this.workOrderDate = workOrderDate;
this.consignee = consignee;
this.sentBy = sentBy;
this.chargeableHead = chargeableHead;
this.accountUnit = accountUnit;
this.division = division;
}
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public String getWorkName() {
return workName;
}
public void setWorkName(String workName) {
this.workName = workName;
}
public String getWorkDescription() {
return workDescription;
}
public void setWorkDescription(String workDescription) {
this.workDescription = workDescription;
}
public String getConsigneeWorkOrder() {
return consigneeWorkOrder;
}
public void setConsigneeWorkOrder(String consigneeWorkOrder) {
this.consigneeWorkOrder = consigneeWorkOrder;
}
public Date getWorkOrderDate() {
return workOrderDate;
}
public void setWorkOrderDate(Date workOrderDate) {
this.workOrderDate = workOrderDate;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getSentBy() {
return sentBy;
}
public void setSentBy(String sentBy) {
this.sentBy = sentBy;
}
public String getChargeableHead() {
return chargeableHead;
}
public void setChargeableHead(String chargeableHead) {
this.chargeableHead = chargeableHead;
}
public String getAccountUnit() {
return accountUnit;
}
public void setAccountUnit(String accountUnit) {
this.accountUnit = accountUnit;
}
public String getFundExist() {
return fundExist;
}
public void setFundExist(String fundExist) {
this.fundExist = fundExist;
}
public String getFundCertificate() {
return fundCertificate;
}
public void setFundCertificate(String fundCertificate) {
this.fundCertificate = fundCertificate;
}
public Double getApproxCost() {
return approxCost;
}
public void setApproxCost(Double approxCost) {
this.approxCost = approxCost;
}
public Double getApproxWeight() {
return approxWeight;
}
public void setApproxWeight(Double approxWeight) {
this.approxWeight = approxWeight;
}
public String getDrawing() {
return drawing;
}
public void setDrawing(String drawing) {
this.drawing = drawing;
}
public String getRequiredAt() {
return requiredAt;
}
public void setRequiredAt(String requiredAt) {
this.requiredAt = requiredAt;
}
public String getRemark1() {
return remark1;
}
public void setRemark1(String remark1) {
this.remark1 = remark1;
}
public String getRemark2() {
return remark2;
}
public void setRemark2(String remark2) {
this.remark2 = remark2;
}
public String getRemark3() {
return remark3;
}
public void setRemark3(String remark3) {
this.remark3 = remark3;
}
public PojoDivision getDivision() {
return division;
}
public void setDivision(PojoDivision division) {
this.division = division;
}
}
從 ViewModel 觸發調用服務器端的方法 -
public void save() {
System.out.println(workDate.getValue());
Date workorderDate = getWorkDateFromString(workDate.getValue());
PojoProject project = new PojoProject(workName.getValue(), workDescription.getValue(), workOrder.getValue(), workorderDate, consignee.getValue(), sentBy.getValue(), chargeableHead.getValue(), accountUnit.getValue(), selectedDivision.getValue());
projectRepository.saveProject(project);
}
這是我的服務器端 Rest End Point,我將 JSON 收集到Project()
對象中並保存到數據庫中——
@POST
@AnnotationSecured
@Path("saveProject")
@Consumes(MediaType.APPLICATION_JSON)
// @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public void saveNewProject(Project newProject) {
System.out.println("inside save new project");
if (newProject.getWorkOrderDate() == null) {
System.out.println("date is null");
} else {
System.out.println("date is " + newProject.getWorkOrderDate().toString());
}
Project project = null;
EntityManager em = UtilityJPA.getEntityManager();
EntityTransaction transaction = em.getTransaction();
try {
transaction.begin();
em.persist(newProject);
transaction.commit();
} catch (Exception ex) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} finally {
em.clear();
}
}
}
您應該嘗試使用DateFormat.FULL
。 像這樣對我來說很好用
Gson gson = new GsonBuilder().setDateFormat(DateFormat.FULL).create();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.