Related
I have a service that listens to a queue then maps it to a POJO.
But I'm always getting this error even after setting the #Configuration of ObjectMapper
#Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(new JavaTimeModule());
return mapper;
}
My POJO:
public class ResultDto {
private ZonedDateTime dateSent;
private ZonedDateTime dateDeliveryReceiptReceived;
public ResultDto() {}
}
I get this error:
Caused by: org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-08-03T11:02:51.044+0000')
Thanks in advance!
Use
#JsonFormat(pattern = 'specify pattern here')
ObjectMapper by default tries to create ZonedDateTime object with String in constructor and such thing does not exists. By adding this annotation you will allow it to parse it from String using given format.
From the error, it looks like it is looking for String-argument constructor. Try after adding the following constructors in ResultDto:
public ResultDto(ZonedDateTime dateSent, ZonedDateTime dateDeliveryReceiptReceived) {
this.dateSent = dateSent;
this.dateDeliveryReceiptReceived = dateDeliveryReceiptReceived;
}
public ResultDto(String dateSent, String dateDeliveryReceiptReceived) {
this.dateSent = ZonedDateTime.parse(dateSent, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
this.dateDeliveryReceiptReceived = ZonedDateTime.parse(dateDeliveryReceiptReceived,
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
}
Thanks for those who answered.
With the help of team mate, we discovered that the spring cloud has its own object mapper. And not the ObjectMapper directly. Since this DTO/POJO is to the message from a AWS SNS/SQS.
This should do:
#Bean
public MappingJackson2MessageConverter mappingJackson2MessageConverter(ObjectMapper objectMapper) {
MappingJackson2MessageConverter jacksonMessageConverter = new MappingJackson2MessageConverter();
jacksonMessageConverter.setObjectMapper(objectMapper);
jacksonMessageConverter.setSerializedPayloadClass(String.class);
jacksonMessageConverter.setStrictContentTypeMatch(true);
return jacksonMessageConverter;
}
We face the same problem by using java 8, Springboot and AWS SQS. We create a custom deserializer which extends from InstantDeserializer for a ZonedDateTime. We apply the custom deserializer directly on the DTO which is associated to the SQS message.
import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class CustomZonedDateTimeDeserializer extends InstantDeserializer<ZonedDateTime> {
public CustomZonedDateTimeDeserializer() {
super(ZonedDateTime.class,
DateTimeFormatter.ISO_ZONED_DATE_TIME,
ZonedDateTime::from,
a -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(a.value), a.zoneId.getId().equals("UTC") ? ZoneOffset.UTC : a.zoneId),
a -> ZonedDateTime.ofInstant(Instant.ofEpochSecond(a.integer, a.fraction), a.zoneId.getId().equals("UTC") ? ZoneOffset.UTC : a.zoneId),
ZonedDateTime::withZoneSameInstant, false);
}
}
In the DTO, we will have:
public class ResultDto {
#JsonDeserialize(using = CustomZonedDateTimeDeserializer.class)
private ZonedDateTime dateSent;
#JsonDeserialize(using = CustomZonedDateTimeDeserializer.class)
private ZonedDateTime dateDeliveryReceiptReceived;
public ResultDto() {}
}
I'm using Spring Boot 2.1.4 and Spring Data Jest with ElasticSearch. I was initially using Java Date for some properties with the following annotation:
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
This is saved into ElasticSearch as follows:
"creationDate": "2019-04-10T14:49:05.672+0000"
Now, I am in the process of migrating from Date to LocalDateTime and ZonedDateTime. When the data is now saved to ElasticSearch, I get the following attribute saved:
"creationDate": {
"dayOfYear": 123,
"dayOfWeek": "FRIDAY",
"month": "MAY",
"dayOfMonth": 3,
"year": 2019,
"monthValue": 5,
"hour": 11,
"minute": 54,
"second": 12,
"nano": 238000000,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
},
What do I need to do to change it so I get the same ElasticSearch data format as before for LocalDateTime and ZonedDateTime?
I have tried the following:
Customising the object mapper as follows:
public class CustomEntityMapper implements EntityMapper {
private final ObjectMapper objectMapper;
public CustomEntityMapper(ObjectMapper objectMapper) {
this.objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.registerModule(new CustomGeoModule());
objectMapper.registerModule(new JavaTimeModule());
}
#Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
}
#Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
}
Adding the following to object mapper:
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
Any help or pointers where I'm going wrong would be appreciated.
Managed to get it to work with Spring Boot 2.1.4 and Spring Data Jest. Here is what I did:
Example domain object:
#Document(indexName = "datetest")
public class DateTest {
#Id
private String id;
#Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ", timezone = "UTC")
private Instant instant = Instant.now();
#Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
private ZonedDateTime zonedDateTime = ZonedDateTime.now();
#Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime localDateTime = LocalDateTime.now();
// getters/setters
}
The ElasticSearch/JEST config:
#Configuration
public class ESConfig {
#Bean
public EntityMapper getEntityMapper() {
return new CustomEntityMapper();
}
#Bean
#Primary
public ElasticsearchOperations elasticsearchTemplate(final JestClient jestClient,
final ElasticsearchConverter elasticsearchConverter,
final SimpleElasticsearchMappingContext simpleElasticsearchMappingContext, EntityMapper mapper) {
return new JestElasticsearchTemplate(jestClient, elasticsearchConverter,
new DefaultJestResultsMapper(simpleElasticsearchMappingContext, mapper));
}
public class CustomEntityMapper implements EntityMapper {
private final ObjectMapper objectMapper;
public CustomEntityMapper() {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.registerModule(new CustomGeoModule());
objectMapper.registerModule(new JavaTimeModule());
}
#Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
}
#Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
}
}
The results in ElasticSearch:
Hope this helps.
That's because spring-data-jest uses DefaultEntityMapper (part of Spring Data), which creates its own ObjectMapper and doesn't use the one provided by Spring boot. This can be seen in this related question.
You're on the right track with your solution by defining your own EntityMapper, for example CustomEntityMapper. However, spring-data-jest wraps this mapper into a class called DefaultJestResultsMapper, which is then used by a bean called JestElasticsearchTemplate.
So, probably you should do something like this:
#Bean
public JestResultsMapper resultMapper(CustomEntityMapper entityMapper) {
return new DefaultJestResultsMapper(entityMapper);
}
#Bean
public JestElasticSearchTemplate template(JestClient client, JestResultsMapper resultsMapper) {
return new JestElasticSearchTemplate(client, resultsMapper);
}
This should inject your CustomEntityMapper into a JestResultsMapper, which is in turn injected into JestElasticSearchTemplate used by the framework.
Within CustomEntityMapper you can either autowire the default ObjectMapper (which will automatically add the JavaTimeModule) or you can configure one on your own.
According to this answer from version 2 of Spring Boot,it should work out of the box as you want in terms of producing string from java.time objects
If you have
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.4.0
as dependency and below line in application.properties
spring.jackson.serialization.write_dates_as_timestamps=false
So only thing left would be to add timezone notation to default string which won't have it.
If standard formatters won't work for you may always write your own serialiser/deserialiser and attach it like explained here
How do I use Jackson JSON mapper with Java 8 LocalDateTime?
org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from JSON String; no single-String constructor/factory method (through reference chain: MyDTO["field1"]->SubDTO["date"])
There's no need to use custom serializers/deserializers here. Use jackson-modules-java8's datetime module:
Datatype module to make Jackson recognize Java 8 Date & Time API data types (JSR-310).
This module adds support for quite a few classes:
Duration
Instant
LocalDateTime
LocalDate
LocalTime
MonthDay
OffsetDateTime
OffsetTime
Period
Year
YearMonth
ZonedDateTime
ZoneId
ZoneOffset
Update: Leaving this answer for historical reasons, but I don't recommend it. Please see the accepted answer above.
Tell Jackson to map using your custom [de]serialization classes:
#JsonSerialize(using = LocalDateTimeSerializer.class)
#JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime ignoreUntil;
provide custom classes:
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
#Override
public void serialize(LocalDateTime arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {
arg1.writeString(arg0.toString());
}
}
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
#Override
public LocalDateTime deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException {
return LocalDateTime.parse(arg0.getText());
}
}
random fact: if i nest above classes and don't make them static, the error message is weird:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
If you are using ObjectMapper class of fasterxml,
by default ObjectMapper do not understand the LocalDateTime class, so, you need to add another dependency in your gradle/maven :
compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.7.3'
Now you need to register the datatype support offered by this library into you objectmapper object, this can be done by following :
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
Now, in your jsonString, you can easily put your java.LocalDateTime field as follows :
{
"user_id": 1,
"score": 9,
"date_time": "2016-05-28T17:39:44.937"
}
By doing all this, your Json file to Java object conversion will work fine, you can read the file by following :
objectMapper.readValue(jsonString, new TypeReference<List<User>>() {
});
This maven dependency will solve your problem:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.4</version>
</dependency>
One thing I've struggled is that for ZonedDateTime timezone being changed to GMT during deserialization.
Turned out, that by default Jackson replaces it with one from context.
To keep zone one must disable this 'feature'
Jackson2ObjectMapperBuilder.json()
.featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
all you need to know is in Jackson Documentation
https://www.baeldung.com/jackson-serialize-dates
Ad.9 quick solved the problem for me.
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
I had a similar problem while using Spring boot.
With Spring boot 1.5.1.RELEASE all I had to do is to add dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
If you can't use jackson-modules-java8 for whatever reasons you can (de-)serialize the instant field as long using #JsonIgnore and #JsonGetter & #JsonSetter:
public class MyBean {
private Instant time = Instant.now();
#JsonIgnore
public Instant getTime() {
return this.time;
}
public void setTime(Instant time) {
this.time = time;
}
#JsonGetter
private long getEpochTime() {
return this.time.toEpochMilli();
}
#JsonSetter
private void setEpochTime(long time) {
this.time = Instant.ofEpochMilli(time);
}
}
Example:
#Test
public void testJsonTime() throws Exception {
String json = new ObjectMapper().writeValueAsString(new MyBean());
System.out.println(json);
MyBean myBean = new ObjectMapper().readValue(json, MyBean.class);
System.out.println(myBean.getTime());
}
yields
{"epochTime":1506432517242}
2017-09-26T13:28:37.242Z
In the newer version of Jackson JSR, e.g., the registerModule(new JSR310Module()) is deprecated, now the suggested one is JavaTimeModule
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonFactory {
private static ObjectMapper objectMapper = null;
public static ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
}
return objectMapper;
}
}
If you are using Jersey then you need to add the Maven dependency (jackson-datatype-jsr310) as the others suggested and register your object mapper instance like so:
#Provider
public class JacksonObjectMapper implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;
public JacksonObjectMapper() {
defaultObjectMapper = createDefaultMapper();
}
#Override
public ObjectMapper getContext(Class<?> type) {
return defaultObjectMapper;
}
private static ObjectMapper createDefaultMapper() {
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return mapper;
}
}
When registering Jackson in your resources, you need to add this mapper like so:
final ResourceConfig rc = new ResourceConfig().packages("<your package>");
rc
.register(JacksonObjectMapper.class)
.register(JacksonJaxbJsonProvider.class);
If you are using Jackson Serializer, here is a way to use the date modules:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.apache.kafka.common.serialization.Serializer;
public class JacksonSerializer<T> implements Serializer<T> {
private final ObjectMapper mapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
#Override
public byte[] serialize(String s, T object) {
try {
return mapper.writeValueAsBytes(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
This is just an example how to use it in a unit test that I hacked to debug this issue.
The key ingredients are
mapper.registerModule(new JavaTimeModule());
maven dependency of <artifactId>jackson-datatype-jsr310</artifactId>
Code:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.Serializable;
import java.time.Instant;
class Mumu implements Serializable {
private Instant from;
private String text;
Mumu(Instant from, String text) {
this.from = from;
this.text = text;
}
public Mumu() {
}
public Instant getFrom() {
return from;
}
public String getText() {
return text;
}
#Override
public String toString() {
return "Mumu{" +
"from=" + from +
", text='" + text + '\'' +
'}';
}
}
public class Scratch {
#Test
public void JacksonInstant() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
Mumu before = new Mumu(Instant.now(), "before");
String jsonInString = mapper.writeValueAsString(before);
System.out.println("-- BEFORE --");
System.out.println(before);
System.out.println(jsonInString);
Mumu after = mapper.readValue(jsonInString, Mumu.class);
System.out.println("-- AFTER --");
System.out.println(after);
Assert.assertEquals(after.toString(), before.toString());
}
}
If you're having this issue because of GraphQL Java Tools and trying to marshal an Java Instant from a date string, you need to setup your SchemaParser to use an ObjectMapper with certain configurations:
In your GraphQLSchemaBuilder class, inject ObjectMapper and add this modules:
ObjectMapper objectMapper =
new ObjectMapper().registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
and add it to the options:
final SchemaParserOptions options = SchemaParserOptions.newOptions()
.objectMapperProvider(fieldDefinition -> objectMapper)
.typeDefinitionFactory(new YourTypeDefinitionFactory())
.build();
See https://github.com/graphql-java-kickstart/graphql-spring-boot/issues/32
For spring boot api :
#Configuration
public class JsonConfig {
#Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);// will remove value properties
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(mapper);
return jsonConverter;
}
}
import the following dependencies :
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.0'
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
add these dependencies and enable these modules. that should help
private static final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
I use this time format: "{birthDate": "2018-05-24T13:56:13Z}" to deserialize from json into java.time.Instant (see screenshot)
You may set this in your application.yml file to resolve Instant time, which is Date API in java8:
spring.jackson.serialization.write-dates-as-timestamps=false
If you are using Spring boot and have this issue with the OffsetDateTime then need to use the registerModules as answered above by #greperror(answered May 28 '16 at 13:04) but note that there is one difference. The dependency mentioned doesn't need to be added as I am guessing that spring boot has it already. I was having this issue with Spring boot and it worked for me without adding this dependency.
If any one having problem while using SpringBoot here is how I fixed the issue without adding new dependency.
In Spring 2.1.3 Jackson expects date string 2019-05-21T07:37:11.000 in this yyyy-MM-dd HH:mm:ss.SSS format to de-serialize in LocalDateTime. Make sure date string separates the date and time with T not with space. seconds (ss) and milliseconds(SSS) could be ommitted.
#JsonProperty("last_charge_date")
public LocalDateTime lastChargeDate;
I wanted to provide support for Spring's DurationStyle parsing, supported in property files in my custom configuration files deserialized using Jackson, like serializing 20s to Duration PT20S. I did this by registering a custom deserializer on the ObjectMapper instance being used for the same:
#Bean("customConfigMapper")
public ObjectMapper customConfigMapper() {
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addDeserializer(Duration.class, new SpringDurationStyleDeserializer());
mapper.registerModule(module);
return mapper;
}
public static class SpringDurationStyleDeserializer extends JsonDeserializer<Duration> {
#Override
public Duration deserialize(JsonParser jsonParser, DeserializationContext __) throws IOException {
return Optional.ofNullable(jsonParser.getText()).map(DurationStyle::detectAndParse).orElse(null);
}
}
ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new JavaTimeModule());
This worked for me
#JsonDeserialize(using = LocalDateTimeDeserializer.class)
#JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime createTime;
This has worked for me.
For those who use Spring Boot 2.x
There is no need to do any of the above - Java 8 LocalDateTime is serialised/de-serialised out of the box. I had to do all of the above in 1.x, but with Boot 2.x, it works seamlessly.
See this reference too JSON Java 8 LocalDateTime format in Spring Boot
Unfortunately, the solution proposed here, didn't work in my environment.
But to be honest, using java8 time objects as DTOs is not a very good idea after all.
I would recommend to create custom DTOs instead, and don't rely on the unstable libraries, which might break after next jdk release. This approach is also in accordance with good practices of anticorruption layer and adapter patterns.
Here is the example of the DTO:
public class ReportDTO implements Serializable {
private YearMonthDTO yearMonth;
public YearMonthDTO getYearMonth() {
return yearMonth;
}
public void setYearMonth(final YearMonthDTO yearMonth) {
this.yearMonth = yearMonth;
}
public void fromYearMonth(final YearMonth yearMonth) {
this.yearMonth = new YearMonthDTO(yearMonth.getYear(),
yearMonth.getMonthValue());
}
}
public static class YearMonthDTO {
private int year;
private int monthValue;
public YearMonthDTO() {
}
public YearMonthDTO(int year, int monthValue) {
this.year = year;
this.monthValue = monthValue;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonthValue() {
return monthValue;
}
public void setMonthValue(int monthValue) {
this.monthValue = monthValue;
}
}
It of course depends on your situation, and the amount of work you would have to do with this solution. As any pattern, this solution is not applicable to all situations.
In any case, the current best answer doesn't seem to work anymore. I didn't try other solutions, but I decided not to rely on any libraries in my simple case.
For those who are looking for a solution on version ES-8 and Spring Boot:3.0
Create a configuration file extending ElasticsearchConfiguration and override clientConfiguration and elasticsearchClient creation.
During elasticsearchClient creation inject your own objectMapper configured to use Java 8 time module, which will override the default objectMapper.
#Override
public ClientConfiguration clientConfiguration() {
return ClientConfiguration.builder()
.connectedTo(<Hostname> +":"+ <Port>)
.usingSsl()
.withBasicAuth(<Username>, <Password>)
.build();
}
#Override
public ElasticsearchClient elasticsearchClient(RestClient restClient) {
Assert.notNull(restClient, "restClient must not be null");
//Create Java8 time module
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateFormat.date_time_no_millis.getPattern())));
//Register the module with objectMapper
ObjectMapper objectMapper=new ObjectMapper()
.registerModule(module);
//To convert datetime to ISO-8601
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//Creating our own jsonpMapper
JsonpMapper jsonpMapper=new JacksonJsonpMapper(objectMapper);
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
restClient, jsonpMapper);
// And create the API client
return new ElasticsearchClient(transport);
}
Maven dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.14.0</version>
</dependency>
If you consider using fastjson, you can solve your problem, note the version
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
I have some questions related to JSON serialization using Jackson in a project where I use Spring Boot 2.0.0.M6, Spring Framework 5.0.1.RELEASE and Jackson 2.9.2.
I have configured the following Jackson-related settings in application.properties:
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
Serialization works mostly as I need. Nevertheless, I have noticed that Jackson seems to cut-off milliseconds if they are 000.
Test 1: Serialize Instant with milliseconds set to 000:
Initialize Instant field using Instant.parse("2017-09-14T04:28:48.000Z")
Serialize it using Jackson
Output will be "2017-09-14T04:28:48Z"
Test 2: Serialize Instant with milliseconds set to some non-000 value:
Initialize Instant field using Instant.parse("2017-09-14T04:28:48.100Z")
Serialize it using Jackson
Output will be "2017-09-14T04:28:48.100Z"
Questions:
Is that behavior by design?
Is there anything I can do to force serialization of 000?
I solve using this aproach:
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(Instant.class, new InstantSerializerWithMilliSecondPrecision());
objectMapper.registerModule(module);
And for InstantSerializerWithMilliSecondPrecision i used this:
public class InstantSerializerWithMilliSecondPrecision extends InstantSerializer {
public InstantSerializerWithMilliSecondPrecision() {
super(InstantSerializer.INSTANCE, false, new DateTimeFormatterBuilder().appendInstant(3).toFormatter());
}
}
Now the Instant serialization always includes milliseconds. Example: 2019-09-27T02:59:59.000Z
There appears to be a Jackson issue open for this here*. That link contains two workarounds
Workaround 1
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
SimpleModule module = new SimpleModule();
module.addSerializer(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
#Override
public void serialize(ZonedDateTime zonedDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeString(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ").format(zonedDateTime));
}
});
objectMapper.registerModule(module);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Workaround 2
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(ZonedDateTime.class,
new ZonedDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")));
ObjectMapper mapper = new ObjectMapper().registerModule(javaTimeModule);
*Link is dead because they deprecated FasterXML/jackson-datatype-jsr310 and moved it to jackson-modules-java8. See https://github.com/FasterXML/jackson-modules-java8/issues/76
If you are trying to do this in Spring Boot and want to use #Gustavo's answer.
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Configuration;
#Configuration
public class AppConfig {
#Bean
public Module javaTimeModule() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(new InstantSerializerWithMilliSecondPrecision());
return module;
}
}
None of two workarounds mentioned by Sean Carroll works me. I end up with writing my own serializer for Instant.
final ObjectMapper mapper = new ObjectMapper();
final JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Instant.class, new KeepMillisecondInstantSerializer());
mapper.registerModule(javaTimeModule);
public class KeepMillisecondInstantSerializer extends JsonSerializer<Instant> {
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.withZone(ZoneId.of("UTC"));
#Override
public void serialize(final Instant instant, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException {
final String serializedInstant = dateTimeFormatter.format(instant);
jsonGenerator.writeString(serializedInstant);
}
}
I guess Jackson use Instant.toString() method to serialize Instant objects by default. I also find some discussions about Instant.toString() method on StackOverflow.
Rather than fixing the bug of Jackson library, following could be a quick work around:
Create a string variable in the POJO class where you have Timestamp variable:
private Timestamp createTimeStamp;
private String stringCreateTimeStamp;
capture timestamp value as a string:
listOfPojo.forEach(pojo-> {
pojo.setStringCreateTimeStamp(request.getcreateTimeStamp().toString());
});
Refer https://www.baeldung.com/java-string-to-timestamp for conversions
Solve it by using custom serializers for LocalDateTime and ZonedDateTime classes.
My solution works for me because I use only these two classes in API responses to represent date and time! I don't use Instant or Date so pay attention on it.
#Configuration
class JacksonConfig {
#Bean
fun objectMapper(): ObjectMapper {
val mapper = ObjectMapper()
val javaTimeModule = JavaTimeModule().apply {
addSerializer(LocalDateTime::class.java, KeepMillisecondLocalDateTimeSerializer())
addSerializer(ZonedDateTime::class.java, KeepMillisecondZonedDateTimeSerializer())
}
mapper.registerModule(javaTimeModule)
return mapper
}
class KeepMillisecondZonedDateTimeSerializer : JsonSerializer<ZonedDateTime>() {
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
#Throws(IOException::class)
override fun serialize(
value: ZonedDateTime,
jsonGenerator: JsonGenerator,
serializerProvider: SerializerProvider?
) {
jsonGenerator.writeString(formatter.format(value))
}
}
class KeepMillisecondLocalDateTimeSerializer : JsonSerializer<LocalDateTime>() {
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
#Throws(IOException::class)
override fun serialize(
value: LocalDateTime,
jsonGenerator: JsonGenerator,
serializerProvider: SerializerProvider?
) {
jsonGenerator.writeString(formatter.format(value))
}
}
}
I have an entity with java.time.Instant for created data field:
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#EqualsAndHashCode
public class Item {
private String id;
private String url;
private Instant createdDate;
}
I am using com.fasterxml.jackson.databind.ObjectMapper to save item to Elasticsearch as JSON:
bulkRequestBody.append(objectMapper.writeValueAsString(item));
ObjectMapper serializes this field as an object:
"createdDate": {
"epochSecond": 1502643595,
"nano": 466000000
}
I was trying the annotation #JsonFormat(shape = JsonFormat.Shape.STRING) but it doesn't work for me.
My question is how I could serialize this field as 2010-05-30 22:15:52 string?
One solution is to use jackson-modules-java8. Then you can add a JavaTimeModule to your object mapper:
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
objectMapper.registerModule(module);
By default the Instant is serialized as the epoch value (seconds and nanoseconds in a single number):
{"createdDate":1502713067.720000000}
You can change that by setting in the object mapper:
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
This will produce the output:
{"createdDate":"2017-08-14T12:17:47.720Z"}
Both formats above are deserialized without any additional configuration.
To change the serialization format, just add a JsonFormat annotation to the field:
#JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTC")
private Instant createdDate;
You need to set the timezone, otherwise the Instant can't be serialized properly (it throws an exception). The output will be:
{"createdDate":"2017-08-14 12:17:47"}
Another alternative, if you don't want to (or can't) use java8 modules, is to create a custom serializer and deserializer, using a java.time.format.DateTimeFormatter:
public class MyCustomSerializer extends JsonSerializer<Instant> {
private DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneOffset.UTC);
#Override
public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
String str = fmt.format(value);
gen.writeString(str);
}
}
public class MyCustomDeserializer extends JsonDeserializer<Instant> {
private DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneOffset.UTC);
#Override
public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return Instant.from(fmt.parse(p.getText()));
}
}
Then you annotate the field with those custom classes:
#JsonDeserialize(using = MyCustomDeserializer.class)
#JsonSerialize(using = MyCustomSerializer.class)
private Instant createdDate;
The output will be:
{"createdDate":"2017-08-14 12:17:47"}
One detail is that in the serialized string you're discarding the fraction of second (everything after the decimal point). So, when deserializing, this information can't be recovered (it'll be set to zero).
In the example above, the original Instant is 2017-08-14T12:17:47.720Z, but the serialized string is 2017-08-14 12:17:47 (without the fraction of seconds), so when deserialized the resulting Instant is 2017-08-14T12:17:47Z (the .720 milliseconds are lost).
For those looking to parse Java 8 timestamps. You need a recent version of jackson-datatype-jsr310 in your POM and have the following module registered:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
To test this code
#Test
void testSeliarization() throws IOException {
String expectedJson = "{\"parseDate\":\"2018-12-04T18:47:38.927Z\"}";
MyPojo pojo = new MyPojo(ZonedDateTime.parse("2018-12-04T18:47:38.927Z"));
// serialization
assertThat(objectMapper.writeValueAsString(pojo)).isEqualTo(expectedJson);
// deserialization
assertThat(objectMapper.readValue(expectedJson, MyPojo.class)).isEqualTo(pojo);
}
Here's some Kotlin code of formatting Instant, so it does not contain milliseconds, you can use custom date formatters
ObjectMapper().apply {
val javaTimeModule = JavaTimeModule()
javaTimeModule.addSerializer(Instant::class.java, Iso8601WithoutMillisInstantSerializer())
registerModule(javaTimeModule)
disable(WRITE_DATES_AS_TIMESTAMPS)
}
private class Iso8601WithoutMillisInstantSerializer
: InstantSerializer(InstantSerializer.INSTANCE, false, DateTimeFormatterBuilder().appendInstant(0).toFormatter())
You need to add below dependency
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.6.5</version>
</dependency>
And then register the modules as below :
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
In my case it was enough to register the JavaTimeModule:
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
objectMapper.registerModule(module);
messageObject = objectMapper.writeValueAsString(event);
In the event Object I have a field of type Instant.
In the deserialization you also need to register the java time module:
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
Event event = objectMapper.readValue(record.value(), Event.class);
You can use Spring ObjectMapper which already configured with JavaTimeModule. Just inject it from Spring context and don't use new ObjectMapper().
If using Spring, and spring-web is on the classpath, you can create an ObjectMapper using the Jackson2ObjectMapperBuilder. It registers the following commonly used modules within the method registerWellKnownModulesIfAvailable.
com.fasterxml.jackson.datatype.jdk8.Jdk8Module
com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
com.fasterxml.jackson.datatype.joda.JodaModule
com.fasterxml.jackson.module.kotlin.KotlinModule
Some of these modules have been merged into Jackson 3; see here.