简体   繁体   English

从post man(休息服务)如何发送json日期(字符串格式)到接受日期对象的java

[英]From post man(rest service) how to send json date(string format) to java which accepts date object

How to bind dateOfJoining value(String type) to member variable "dateOfJoining"(Date type) in "DateInput" Class after sending below JSON through postman. 如何通过postman发送下面的JSON后,将dateOfJoining值(String类型)绑定到“DateInput”类中的成员变量“dateOfJoining”(日期类型)。 How to convert String to Date object in java with the same format dd/MM/yyyy. 如何使用相同的格式dd / MM / yyyy将String中的String转换为Date对象。 Converted date should be in Date object but not String. 转换日期应该在Date对象中,而不是String。

Json is as given below Json如下所示

{
 "dateOfJoining" : "03/04/2017"
}

Service URL hitting in postman -- localhost/Rest/hello 邮局中的服务URL命中 - localhost / Rest / hello

RestService class in java - HandleRestRequest.java java中的RestService类 - HandleRestRequest.java

@RestController
public class HandleRestRequest
{

  @RequestMapping("\hello");
  public List setRequestParams(@RequestBody DateInput dateInput)
 {
   .......
  }
 }

 Pojo Class DateInput.java

  public class DateInput
 {
  private  Date dateOfJoining;
   .......
  }

If I send date from json in below format, its throwing error as unsatisfied input 如果我以下面的格式从json发送日期,其投掷错误为不满意的输入

 {
  "dateOfJoining" : 04/04/2017
 }

So I sending it as string format and by changing dateOfJoining as string in DateInput.java file and later I tried to convert it as date object as below 所以我将它作为字符串格式发送,并通过在DateInput.java文件中将dateOfJoining更改为字符串,稍后我尝试将其转换为日期对象,如下所示

Modified DateInput.java file from Date to String 修改DateInput.java文件,从Date到String

 public class DateInput
 {
  private  String dateOfJoining;
   .......
 }

Modified JSON 修改了JSON

{
 "dateOfJoining" : "04/04/2017"
}

Code to convert user input from String to Date 用于将用户输入从String转换为Date的代码

 DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  String convertedDate = sdf.format(dateInput.getDateOfJoining());

Its converting into required format but return type is String but expected is Date object to send DAO layer. 它转换为所需的格式但返回类型是String但预期是发送DAO层的Date对象。 So I tried sdf.parse, its returning Date object but not in required format 所以我尝试了sdf.parse,它返回Date对象但不是必需的格式

Date date = sdf.parse(sdf.format(dateInput.getDateOfJoining()));
output is like -  Tue Apr 04 00:00:00 IST 2017
expected is 04/04/2017 (with return type Date object).

So please help me how to convert the string to date object with required format since DAO layer is expecting input as date object in format dd/MM/yyyy 所以请帮助我如何将字符串转换为具有所需格式的日期对象,因为DAO层期望以格式dd / MM / yyyy作为日期对象输入

Edit: Updating answer in accordance with updated question. 编辑:根据更新的问题更新答案。

Use annotation @JsonFormat from Jackson Databind to specify the date pattern. 使用Jackson Databind中的注释@JsonFormat指定日期模式。

public class DateInput
 {
  @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
  private  Date dateOfJoining;
   .......
  }

change your code to the below code snippet 将您的代码更改为以下代码段

public List setRequestParams(@RequestParam("dateOfJoining")@DateTimeFormat(pattern="dd-MM-yyyy")  DateInput dateInput)
{
   ...
}

With JSON-B (included in Java EE 8) you can do this: 使用JSON-B(包含在Java EE 8中),您可以执行以下操作:

class DateInput {
    @JsonbDateFormat("dd/MM/yyyy")
    public Date dateOfJoining;
}

In my tests with Thorntail 2.4 I don't need any annotation for ISO format when using java.util.Date : 在使用Thorntail 2.4的测试中,使用java.util.Date时,我不需要任何ISO格式的注释:

{
    "dateOfJoining" : "2019-04-28T14:45:15"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM