Ale*_*ser 9 java spring http-status-code-400 swift zoneddatetime
我希望得到一些帮助调试此问题.如果我将以下JSON发送到我的后端,它可以正常工作:
{
"approvalRequired": false,
"location": {
"locationName": "<+37.33233141,-122.03121860> +\/- 5.00m (speed 0.00 mps \/ course -1.00) @ 9\/16\/18, 9:24:59 PM Pacific Daylight Time",
"longitude": -122.0312186,
"latitude": 37.332331410000002
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我现在发送以下内容:
{
"approvalRequired": false,
"scheduledStartTime": "2016-01-01T10:24:00+01:00",
"location": {
"locationName": "<+37.33233141,-122.03121860> +\/- 5.00m (speed 0.00 mps \/ course -1.00) @ 9\/16\/18, 9:24:59 PM Pacific Daylight Time",
"longitude": -122.0312186,
"latitude": 37.332331410000002
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了上述错误.在我的后端代码中,我有以下内容:
@DynamoDBTypeConverted(converter = ZonedDateTimeTypeConverter.class)
@DynamoDBAttribute(attributeName = "scheduledStartTime")
public ZonedDateTime scheduledStartTime;
Run Code Online (Sandbox Code Playgroud)
API方法签名如下所示:
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity create(@RequestBody Event event) {...}
Run Code Online (Sandbox Code Playgroud)
我相信我遇到的问题是无法将JSON解析为ZonedDateTime.有没有人有任何建议,(1)ZonedDateTime自动接受json字符串格式的时间或(2)如何使DTO解析分区日期时间?
谢谢!
小智 3
假设您使用的是最“默认”的配置,它基于FasterXML Jackson。
如果是这样,那么您只需ZonedDateTime在应用程序中正确配置串行器和反串行器即可;它可能是自定义的,也可能是jackson-datatype-jsr310 中的(推荐)。
我创建了一个小型/最小示例,它基于Spring 5.0.9和Jackson 2.9.6(当前最新版本)。
请在这里找到:spring5-rest-zoneddatetime >>,主要部分是:
Event数据传输对象:
public class Event {
private long id;
private String name;
private ZonedDateTime time;
// Constructors, public getters and setters
}
Run Code Online (Sandbox Code Playgroud)
字段time可能public与您的示例相同,这也很好,但如果字段是private- 那么您将需要publicgetter 和 setter。
注意:我忽略此处@DynamoDBTypeConverted和@DynamoDBAttribute注释,因为它们与持久性逻辑相关,而不是 REST 层。
EventController仅包含一种与您相同的方法:
@RestController
public class EventController {
@RequestMapping(value = "/event", method = RequestMethod.POST)
public ResponseEntity post(@RequestBody Event event) {
System.out.println("Event posted: " + event.toString());
return ResponseEntity.ok(event);
}
}
Run Code Online (Sandbox Code Playgroud)外观上的依赖关系pom.xml如下:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这里重要的是JSR-310数据类型实现,它还引入了com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer和com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer。
如果需要自定义序列化器/反序列化器,请检查此问题>>
该字段将支持下一个日期格式time:
"2018-01-01T22:25:15+01:00[Europe/Paris]"-顺便说一句,不完全是ISO 8601"2018-01-01T22:25:15+01:00""2018-01-01T22:25:15.000000001Z"1514768461.000000001- 浮点数字,从1970-01-01, 00:00:00 [UTC]默认情况下,REST APi 响应将使用浮点数字来表示日期,例如在我们的示例中,响应将如下所示:
{
"id": 3,
"name": "Test event",
"time": 1514768460
}
Run Code Online (Sandbox Code Playgroud)
要返回字符串值,请检查例如这个问题>>
还需要提到的是,如果您将使用Spring Boot(很好的入门者)——上面讨论的所有内容都将开箱即用。
| 归档时间: |
|
| 查看次数: |
255 次 |
| 最近记录: |