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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43227251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:47:30  来源:igfitidea点击:

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

javajsonrestjava.util.date

提问by Phalani Kumar

How to bind dateOfJoining value(String type) to member variable "dateOfJoining"(Date type) in "DateInput" Class after sending below JSON through postman. How to convert String to Date object in java with the same format dd/MM/yyyy. Converted date should be in Date object but not String.

通过邮递员在 JSON 下发送后,如何将 dateOfJoining 值(字符串类型)绑定到“DateInput”类中的成员变量“dateOfJoining”(日期类型)。如何在java中将String转换为具有相同格式dd/MM/yyyy的Date对象。转换后的日期应该在 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

将用户输入从字符串转换为日期的代码

 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. So I tried sdf.parse, its returning Date object but not in required format

它转换为所需的格式,但返回类型是字符串,但预期是发送 DAO 层的日期对象。所以我尝试了 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 的日期对象

回答by santosh-patil

Edit: Updating answer in accordance with updated question.

编辑:根据更新的问题更新答案。

Use annotation @JsonFormatfrom Hymanson Databindto specify the date pattern.

使用注释@JsonFormatHyman逊数据绑定到指定的日期模式。

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

回答by srikanth

change your code to the below code snippet

将您的代码更改为以下代码片段

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

回答by Panu Haaramo

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 的测试中,我在使用时不需要任何 ISO 格式的注释java.util.Date

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