spring-data-rest和controllers,使用相同的objectMaper进行序列化/反序列化 [英] spring-data-rest and controllers, use the same objectMaper for serializing/deserializing

查看:299
本文介绍了spring-data-rest和controllers,使用相同的objectMaper进行序列化/反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在spring-data-rest的文档中发现它使用了自己的objectMapper实现。

I have found in the documentation of spring-data-rest that it use it's own objectMapper implementation.

我想知道是否可以重用这个objectMapper所以我可以拥有与相同的实体表示形式spring-data-rest 端点

I would like to know if it's possible to reuse this objectMapper so I can have the same entity representation as in spring-data-rest endpoint

例如,没有任何杰克逊objectMapper bean我有这个

For example, without any jackson objectMapper bean I have this

端点:GET / api / companies

Endpoint: GET /api/companies


createdDate:{
content:2016-12-25T12:39:03.437Z
},
lastModifiedDate:null,
createdById :null,
lastModifiedById:null,
active:true,
name:A6,
addressSecondary:null,
foundingDate:{
content:2016-01-01
},

但对于我的控制器我有:

But for my controller I have :

"createdDate": {
    "nano": 437000000,
    "epochSecond": 1482669543
},
"lastModifiedDate": null,
"createdById": null,
"lastModifiedById": null,
"active": true,
"name": "A6",
"addressSecondary": null,
"foundingDate": {
    "year": 2016,
    "month": "JANUARY",
    "era": "CE",
    "dayOfYear": 1,
    "dayOfWeek": "FRIDAY",
    "leapYear": true,
    "dayOfMonth": 1,
    "monthValue": 1,
    "chronology": {
    "calendarType": "iso8601",
    "id": "ISO"
}

这是我自己的控制器实现:

This is my own controller implementation :

@RequestMapping(method = RequestMethod.GET, value = "companies", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<?> testRead() {
    List<Customer> customerRepositoryList = customerRepository.findAll();
    Resources<Customer> resources = new Resources<>(customerRepositoryList);
    return ResponseEntity.ok(resources);
}

我的代码中没有任何用于任何objectMapper的bean。

I have no bean for any objectMapper in my code.

如何获得相同的序列化?

How can I get the same serialization ?

推荐答案

我想你只想输出 LocalDate LocalDateTime 字段正确无误。
如果是这样 - 将依赖项添加到项目中:

I think you just want to output LocalDate and LocalDateTime fields correctly. If so - add to your project this dependencies:

<!-- JDK 8 DateTime support for Jackson -->
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

以及这些对字段的注释:

And these annotations to fields:

@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate date;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime dateTime;

这篇关于spring-data-rest和controllers,使用相同的objectMaper进行序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆