2016-12-25 74 views
0

我在spring-data-rest的文档中发现它使用它自己的objectMapper实现。spring-data-rest和控制器,使用相同的objectMaper进行序列化/反序列化

我想知道是否有可能重新使用此objectMapper,所以我可以有相同的实体表示在spring-data-rest端点

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

端点:GET/API /公司

"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" },

但我的控制,我有:

"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" 
} 

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

@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在我的代码。

如何获得相同的序列号?

回答

1

我想你只是想输出LocalDateLocalDateTime字段正确。 如果是这样的 - 添加到您的项目这依赖关系:

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

而且这些注释字段:

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

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