- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 Android(Retrofit)向服务器发送请求正文(由 JAVA/JERSEY/JACKSON 支持)。我正在通过拦截器记录请求正文,它看起来像什么
{"workId":"456655","workName":"some work","workOrderDate":"Jan 29, 2020 12:00:00 AM"}
在服务器端,我的Project
对象能够解析除“workOrderDate”
之外的每个字段。我尝试使用
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();
但仍采用 JSON,格式未更改(2020 年 1 月 29 日 12:00:00 AM),并且未分配给 Project
对象的 "workOrderDate"
字段在服务器端。
Servier 端的对象-
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 + " ]";
}
}
Android端
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);
}
这是我的服务器端休息端点,我将 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();
关于java - 为什么我无法将 JSON (GSON) 日期格式从改造请求正文转换为 java 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59890152/
我正在使用 Retrofit 和 RxJava,但似乎无法做我想做的事。 这是我对 Web 服务的声明: Observable rawRemoteDownload(@Header("Cookie")
我正在开发一个android应用程序,并使用改造发送请求和获取响应。像这样: public interface Service { @POST("/GetInfo") InfoResp
我刚刚从响应中获取代码,它说我的请求参数错误,那么我的 api 调用应该是什么样子? 这是来自文档的硬编码 API 调用 https://api.themoviedb.org/3/discover/m
实际上,当我使用具有 radio 频率的设备时,我的应用程序出现了问题。如果设备超出 radio 范围但尝试发送文件,我会收到 onFailure 消息,我对用户说没有网络,但主要问题是,一旦设备返回
我一直在关注其他答案,但缺少一个我找不到的步骤,这导致调用成功但数据未被正确解析,因为我进行的第一次调用返回了一个对象列表,但只返回了一个对象全部为空 我的模型.java public cla
对于我正在使用的服务器,我们有一个子域和一个目录,它们都绑定(bind)在一起。使用 Retrofit,您需要指定 baseURL,它似乎不允许目录。有什么方法可以实现吗? 例子: https://d
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我对 Android 还很陌生.. 我正在尝试使用 Retrofit 从 MySQL 检索数据.. 我在代码中没有发现任何错误,但是当我从设备运行应用程序时,它卡在“正在获取数据的进度对话框”上...
我正在使用 Dagger2 + Retrofit + RxAndroid + OkHttp3 + 新架构组件开发一个 Android 应用程序。 最小 sdk = 16。 问题:在 API 16 上运
我需要在插入硬编码查询后设置查询。 我的 API 地址是: myapiaddress/names?q=Yoni&gender=Man&(here i need to enter dynamic
我正在创建天气应用程序,让用户可以选择按任何城市搜索天气。当用户输入有效的城市名称时,我的应用程序工作正常,但当用户输入无效的字符串 ex: asndfs,bdfbsj 然后我的应用程序终止。 如何处
我有一个 Json 字符串。我无法使用带有两个列表(类别和产品)的 Retrofit onResponse。我怎么打电话,回调?我应该用什么?通常不是列表,对吧? { "Categories": [{
我正在实现一个两级嵌套的 recyclerView 并且两个回收器 View 都使用 retrofit 进行 API 调用。这是发出同步请求的方法: public void loadSectionSt
我正在使用 Qt 编写一个应用程序并且想要一个“Metro 风格”的界面。一切都完成了,除了我不知道如何让小部件出现和消失。例如,在 WPF 中,您可以为 (UIElement.RenderTrans
我在 java 应用程序中使用 Retrofit 来访问 api-rest。 我使用简单的代码: RestAdapter.Builder().setEndpoint(uri).setLogLevel(
我需要将下面报告的数据字符串转换为以时间戳(第一个数字元素)为键的字典。我该怎么做? element=20151201091000|22844.4|22786.2|22801.6|22839.7|10
我正在尝试使用 @QueryMap 发送多个参数(就像我通常做的那样)但是这次使用改造通过 POST。 改造 API @POST("/request.php") void sendRequest(@Q
我正在使用 Retrofit 进行后端通信:如果状态码不是 200 则回调调用失败方法。但是我想在失败方法中获取状态代码以进行进一步的代码调节 @Override pu
我正在使用自定义记录器记录到 Logcat 和文件(当文件启用时)。 这在接收来自测试人员的错误报告时非常有用(因为我在应用程序中还有一个按钮可以发送错误报告并附上日志)。 问题:我正在使用 Retr
很抱歉,如果我的标题含糊不清,但我找不到更好的。 我有一个以这种方式公开服务的休息 Api:/api/{type}/{id} 4 个“类型”,因此返回 4 个类类型。 所有这些类都继承自同一个父类(s
我是一名优秀的程序员,十分优秀!