简体   繁体   English

如何从SpringBoot的RestController中的REST请求中获取Calendar字段?

[英]How to get Calendar field from REST request in RestController in SpringBoot?

I am getting date in POST request in @RestController with Json like this: { "date":"2016-01-28" } How can I get object with type java.util.Calendar ? 我在Json的@RestController中的POST请求中获取日期,如下所示: { "date":"2016-01-28" }如何获取类型为java.util.Calendar对象? Usually I use java.util.Date , but almost all methods are deprecated. 通常我使用java.util.Date ,但是几乎所有方法都已弃用。 What Jackson annotation possible to use to get Calendar ? 可以使用什么Jackson注释来获取Calendar I tried to use 我尝试使用

public class Test {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    Calendar date;
}

but get null 但得到null

You can use annotation @DateTimeFormat . 您可以使用注释@DateTimeFormat According to Spring docs it's applicable to java.util.Calendar . 根据Spring 文档,它适用于java.util.Calendar

@PostMapping("/somemapping")
public void date(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Calendar date) {
   //some other processing code
}

I would suggest using a java.util.Date instead of a Calendar . 我建议使用java.util.Date代替Calendar At least in your repository object. 至少在您的存储库对象中。 If you need the additional functionality of a Calendar you can create an instance as demonstrated by @Hamza . 如果您需要日历的其他功能,则可以创建一个实例,如@Hamza

Calendar is a pretty heavy-weight class. Calendar是一门非常重量级的课程。 No need to create a new instance of it considering the tiny amount of data you are storing. 考虑到要存储的数据量很少,因此无需创建新实例。

You can pick out the date from another class that operates on it to provide you the additional functionality as required. 您可以从对其进行操作的另一个类中选择日期,以根据需要为您提供其他功能。

{
"calendar": 1558347802873  
}

Sample json request 样本JSON请求

just pass date in number format use below object 只是以数字格式传递日期,在对象下面使用

import java.util.Calendar;

public class TestCal {

private Calendar calendar;

public Calendar getCalendar() {
    return calendar;
}

public void setCalendar(Calendar calendar) {
    this.calendar = calendar;
}
}

sample spring Controller 样品弹簧控制器

    @PostMapping("/testCal")
    public void testCal(@RequestBody TestCal test) {

    // do your stuff
    test.getCalendar();


   }

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

相关问题 错误404-使用@RestController获取请求-SpringBoot - Error 404 - Get request using @RestController - SpringBoot 如何在springboot(REST)的ExceptionHandler类中获取请求体数据 - How to get the request body data inside ExceptionHandler class in springboot(REST) 如何修复 SpringBoot 中 RestController 中的转换器? - How to fix converter in RestController in SpringBoot? 如何为 RestController 配置 SpringBoot 认证 - How to configure SpringBoot authentication for RestController 如何在 Spring Boot RestController 中获取请求 URL - How to get request URL in Spring Boot RestController SpringBoot获取应用的@RestController列表即List&lt;@RestController&gt; - SpringBoot get the list of @RestController i.e List<@RestController> of the application Springboot,使用Rest Controller从对象访问字段 - Springboot, accessing field from object with Rest Controller 如何从Web服务请求中获取日历对象? - How to get Calendar Object from webservice request? 如何从 Validated Spring RestController 获取多个缺失请求参数的详细信息? - How do I get the details of multiple missing request parameters from a Validated Spring RestController? 强制字段的 Json 请求验证 - springboot rest-api - Json request validation for mandatory field - springboot rest-api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM