Spring Boot 2 not serializing LocalDateTime
up vote
2
down vote
favorite
I recently tried to implement a micro service using spring-boot 2.
Now, whenever I attempt to return an object which contains a java.time.LocalDateTime
from my REST service, the LocalDateTime get serialized as an array of integers. Like so:
{
"id": "5bf1425f9f8de267f04b22ad",
"description": "aaaaaarrrgggghhhhh",
"timestamp": [
2018,
11,
18,
11,
43,
43,
889000000
],
"time": 2.25,
...
}
I have tried configuring the ObjectMapper
through settings in application.yml
spring:
jackson:
serialization:
write-dates-as-timestamps: false
but doesn't work. I have also tried configuring a new ObjectMapper through a Spring Configuration class, like so:
@Configuration
public class JacksonConfig {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
final ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
My configuration gets loaded (debugger stops at a breakpoint) - it's just that it does nothing.
I tried adding jackson
dependencies manually (also for the jsr310 module) to my pom.xml - also without any luck.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
For some reason it looks like Spring Boot is ignoring my attempts to anything with the ObjectMapper, and it keeps returning the same result.
Setting log level to DEBUG for com.fasterxml
in the application.yml
also yields no output:
logging:
level:
com.fasterxml: DEBUG
I use Spring Boot 2.1.0-RELEASE with Jackson 2.9.7.
The basic pom file was generated from https://start.spring.io My project compiles for and runs on a Java 8 JVM.
java spring spring-boot datetime jackson2
add a comment |
up vote
2
down vote
favorite
I recently tried to implement a micro service using spring-boot 2.
Now, whenever I attempt to return an object which contains a java.time.LocalDateTime
from my REST service, the LocalDateTime get serialized as an array of integers. Like so:
{
"id": "5bf1425f9f8de267f04b22ad",
"description": "aaaaaarrrgggghhhhh",
"timestamp": [
2018,
11,
18,
11,
43,
43,
889000000
],
"time": 2.25,
...
}
I have tried configuring the ObjectMapper
through settings in application.yml
spring:
jackson:
serialization:
write-dates-as-timestamps: false
but doesn't work. I have also tried configuring a new ObjectMapper through a Spring Configuration class, like so:
@Configuration
public class JacksonConfig {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
final ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
My configuration gets loaded (debugger stops at a breakpoint) - it's just that it does nothing.
I tried adding jackson
dependencies manually (also for the jsr310 module) to my pom.xml - also without any luck.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
For some reason it looks like Spring Boot is ignoring my attempts to anything with the ObjectMapper, and it keeps returning the same result.
Setting log level to DEBUG for com.fasterxml
in the application.yml
also yields no output:
logging:
level:
com.fasterxml: DEBUG
I use Spring Boot 2.1.0-RELEASE with Jackson 2.9.7.
The basic pom file was generated from https://start.spring.io My project compiles for and runs on a Java 8 JVM.
java spring spring-boot datetime jackson2
did you try to create an ObjectMapper instead of passing a builder to method args and then build a new one based on it? mapper = new ObjectMapper() and then customize it
– slimane
14 hours ago
Just created a demo project with spring-initializr, (Java 8, Maven, Web support). I ddn't do anything other than add a controller and a simple class with a LocalDateTime property, and I can't reproduce. Are you sure you don't have other dependencies that would change the configuration of Jackson?
– JB Nizet
14 hours ago
2
Do you have@EnableWebMvc
on one of your@Configuration
classes? If so, remove it. It fights with the spring boot way of configuring spring mvc.
– teppic
12 hours ago
@JBNizet I think you may be on to something. For this project I'm using mongodb - it may be that the spring-starter-mongo serializer/deserializer is the culprit - I'll get back when I have tried to remove it.
– Martin Jes Rasmussen
4 hours ago
@teppic +1 Bingo that was the problem - now I hope my Cors config still works... Too bad I can't accept your response as a solution. :)
– Martin Jes Rasmussen
4 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I recently tried to implement a micro service using spring-boot 2.
Now, whenever I attempt to return an object which contains a java.time.LocalDateTime
from my REST service, the LocalDateTime get serialized as an array of integers. Like so:
{
"id": "5bf1425f9f8de267f04b22ad",
"description": "aaaaaarrrgggghhhhh",
"timestamp": [
2018,
11,
18,
11,
43,
43,
889000000
],
"time": 2.25,
...
}
I have tried configuring the ObjectMapper
through settings in application.yml
spring:
jackson:
serialization:
write-dates-as-timestamps: false
but doesn't work. I have also tried configuring a new ObjectMapper through a Spring Configuration class, like so:
@Configuration
public class JacksonConfig {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
final ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
My configuration gets loaded (debugger stops at a breakpoint) - it's just that it does nothing.
I tried adding jackson
dependencies manually (also for the jsr310 module) to my pom.xml - also without any luck.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
For some reason it looks like Spring Boot is ignoring my attempts to anything with the ObjectMapper, and it keeps returning the same result.
Setting log level to DEBUG for com.fasterxml
in the application.yml
also yields no output:
logging:
level:
com.fasterxml: DEBUG
I use Spring Boot 2.1.0-RELEASE with Jackson 2.9.7.
The basic pom file was generated from https://start.spring.io My project compiles for and runs on a Java 8 JVM.
java spring spring-boot datetime jackson2
I recently tried to implement a micro service using spring-boot 2.
Now, whenever I attempt to return an object which contains a java.time.LocalDateTime
from my REST service, the LocalDateTime get serialized as an array of integers. Like so:
{
"id": "5bf1425f9f8de267f04b22ad",
"description": "aaaaaarrrgggghhhhh",
"timestamp": [
2018,
11,
18,
11,
43,
43,
889000000
],
"time": 2.25,
...
}
I have tried configuring the ObjectMapper
through settings in application.yml
spring:
jackson:
serialization:
write-dates-as-timestamps: false
but doesn't work. I have also tried configuring a new ObjectMapper through a Spring Configuration class, like so:
@Configuration
public class JacksonConfig {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
final ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
My configuration gets loaded (debugger stops at a breakpoint) - it's just that it does nothing.
I tried adding jackson
dependencies manually (also for the jsr310 module) to my pom.xml - also without any luck.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
For some reason it looks like Spring Boot is ignoring my attempts to anything with the ObjectMapper, and it keeps returning the same result.
Setting log level to DEBUG for com.fasterxml
in the application.yml
also yields no output:
logging:
level:
com.fasterxml: DEBUG
I use Spring Boot 2.1.0-RELEASE with Jackson 2.9.7.
The basic pom file was generated from https://start.spring.io My project compiles for and runs on a Java 8 JVM.
java spring spring-boot datetime jackson2
java spring spring-boot datetime jackson2
edited 1 hour ago
Samuel J Mathew
3,2942228
3,2942228
asked 15 hours ago
Martin Jes Rasmussen
637
637
did you try to create an ObjectMapper instead of passing a builder to method args and then build a new one based on it? mapper = new ObjectMapper() and then customize it
– slimane
14 hours ago
Just created a demo project with spring-initializr, (Java 8, Maven, Web support). I ddn't do anything other than add a controller and a simple class with a LocalDateTime property, and I can't reproduce. Are you sure you don't have other dependencies that would change the configuration of Jackson?
– JB Nizet
14 hours ago
2
Do you have@EnableWebMvc
on one of your@Configuration
classes? If so, remove it. It fights with the spring boot way of configuring spring mvc.
– teppic
12 hours ago
@JBNizet I think you may be on to something. For this project I'm using mongodb - it may be that the spring-starter-mongo serializer/deserializer is the culprit - I'll get back when I have tried to remove it.
– Martin Jes Rasmussen
4 hours ago
@teppic +1 Bingo that was the problem - now I hope my Cors config still works... Too bad I can't accept your response as a solution. :)
– Martin Jes Rasmussen
4 hours ago
add a comment |
did you try to create an ObjectMapper instead of passing a builder to method args and then build a new one based on it? mapper = new ObjectMapper() and then customize it
– slimane
14 hours ago
Just created a demo project with spring-initializr, (Java 8, Maven, Web support). I ddn't do anything other than add a controller and a simple class with a LocalDateTime property, and I can't reproduce. Are you sure you don't have other dependencies that would change the configuration of Jackson?
– JB Nizet
14 hours ago
2
Do you have@EnableWebMvc
on one of your@Configuration
classes? If so, remove it. It fights with the spring boot way of configuring spring mvc.
– teppic
12 hours ago
@JBNizet I think you may be on to something. For this project I'm using mongodb - it may be that the spring-starter-mongo serializer/deserializer is the culprit - I'll get back when I have tried to remove it.
– Martin Jes Rasmussen
4 hours ago
@teppic +1 Bingo that was the problem - now I hope my Cors config still works... Too bad I can't accept your response as a solution. :)
– Martin Jes Rasmussen
4 hours ago
did you try to create an ObjectMapper instead of passing a builder to method args and then build a new one based on it? mapper = new ObjectMapper() and then customize it
– slimane
14 hours ago
did you try to create an ObjectMapper instead of passing a builder to method args and then build a new one based on it? mapper = new ObjectMapper() and then customize it
– slimane
14 hours ago
Just created a demo project with spring-initializr, (Java 8, Maven, Web support). I ddn't do anything other than add a controller and a simple class with a LocalDateTime property, and I can't reproduce. Are you sure you don't have other dependencies that would change the configuration of Jackson?
– JB Nizet
14 hours ago
Just created a demo project with spring-initializr, (Java 8, Maven, Web support). I ddn't do anything other than add a controller and a simple class with a LocalDateTime property, and I can't reproduce. Are you sure you don't have other dependencies that would change the configuration of Jackson?
– JB Nizet
14 hours ago
2
2
Do you have
@EnableWebMvc
on one of your @Configuration
classes? If so, remove it. It fights with the spring boot way of configuring spring mvc.– teppic
12 hours ago
Do you have
@EnableWebMvc
on one of your @Configuration
classes? If so, remove it. It fights with the spring boot way of configuring spring mvc.– teppic
12 hours ago
@JBNizet I think you may be on to something. For this project I'm using mongodb - it may be that the spring-starter-mongo serializer/deserializer is the culprit - I'll get back when I have tried to remove it.
– Martin Jes Rasmussen
4 hours ago
@JBNizet I think you may be on to something. For this project I'm using mongodb - it may be that the spring-starter-mongo serializer/deserializer is the culprit - I'll get back when I have tried to remove it.
– Martin Jes Rasmussen
4 hours ago
@teppic +1 Bingo that was the problem - now I hope my Cors config still works... Too bad I can't accept your response as a solution. :)
– Martin Jes Rasmussen
4 hours ago
@teppic +1 Bingo that was the problem - now I hope my Cors config still works... Too bad I can't accept your response as a solution. :)
– Martin Jes Rasmussen
4 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
You must add jsr310 support to jackson :
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
New contributor
Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
– Ole V.V.
5 hours ago
Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.
– Martin Jes Rasmussen
4 hours ago
add a comment |
up vote
0
down vote
This answer is based on teppic's comment to the original post.
The issue was caused by @EnableWebMVC on one of my @Configuration classes. Removed @EnableWebMVC immediately solved the problem.
add a comment |
up vote
0
down vote
From your comments I found the issue as you blend the @EnableWebMvc
with sprongboot. It turns out that Spring Boot doesn’t mix well with the standard Spring MVC @EnableWebMvc
. What happens when you add the annotation is that spring boot autoconfiguration is disabled.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You must add jsr310 support to jackson :
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
New contributor
Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
– Ole V.V.
5 hours ago
Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.
– Martin Jes Rasmussen
4 hours ago
add a comment |
up vote
0
down vote
You must add jsr310 support to jackson :
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
New contributor
Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
– Ole V.V.
5 hours ago
Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.
– Martin Jes Rasmussen
4 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
You must add jsr310 support to jackson :
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
New contributor
You must add jsr310 support to jackson :
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
New contributor
New contributor
answered 14 hours ago
Zomzog
12
12
New contributor
New contributor
Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
– Ole V.V.
5 hours ago
Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.
– Martin Jes Rasmussen
4 hours ago
add a comment |
Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
– Ole V.V.
5 hours ago
Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.
– Martin Jes Rasmussen
4 hours ago
Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
– Ole V.V.
5 hours ago
Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
– Ole V.V.
5 hours ago
Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.
– Martin Jes Rasmussen
4 hours ago
Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.
– Martin Jes Rasmussen
4 hours ago
add a comment |
up vote
0
down vote
This answer is based on teppic's comment to the original post.
The issue was caused by @EnableWebMVC on one of my @Configuration classes. Removed @EnableWebMVC immediately solved the problem.
add a comment |
up vote
0
down vote
This answer is based on teppic's comment to the original post.
The issue was caused by @EnableWebMVC on one of my @Configuration classes. Removed @EnableWebMVC immediately solved the problem.
add a comment |
up vote
0
down vote
up vote
0
down vote
This answer is based on teppic's comment to the original post.
The issue was caused by @EnableWebMVC on one of my @Configuration classes. Removed @EnableWebMVC immediately solved the problem.
This answer is based on teppic's comment to the original post.
The issue was caused by @EnableWebMVC on one of my @Configuration classes. Removed @EnableWebMVC immediately solved the problem.
answered 2 hours ago
Martin Jes Rasmussen
637
637
add a comment |
add a comment |
up vote
0
down vote
From your comments I found the issue as you blend the @EnableWebMvc
with sprongboot. It turns out that Spring Boot doesn’t mix well with the standard Spring MVC @EnableWebMvc
. What happens when you add the annotation is that spring boot autoconfiguration is disabled.
add a comment |
up vote
0
down vote
From your comments I found the issue as you blend the @EnableWebMvc
with sprongboot. It turns out that Spring Boot doesn’t mix well with the standard Spring MVC @EnableWebMvc
. What happens when you add the annotation is that spring boot autoconfiguration is disabled.
add a comment |
up vote
0
down vote
up vote
0
down vote
From your comments I found the issue as you blend the @EnableWebMvc
with sprongboot. It turns out that Spring Boot doesn’t mix well with the standard Spring MVC @EnableWebMvc
. What happens when you add the annotation is that spring boot autoconfiguration is disabled.
From your comments I found the issue as you blend the @EnableWebMvc
with sprongboot. It turns out that Spring Boot doesn’t mix well with the standard Spring MVC @EnableWebMvc
. What happens when you add the annotation is that spring boot autoconfiguration is disabled.
answered 1 hour ago
Samuel J Mathew
3,2942228
3,2942228
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364800%2fspring-boot-2-not-serializing-localdatetime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
did you try to create an ObjectMapper instead of passing a builder to method args and then build a new one based on it? mapper = new ObjectMapper() and then customize it
– slimane
14 hours ago
Just created a demo project with spring-initializr, (Java 8, Maven, Web support). I ddn't do anything other than add a controller and a simple class with a LocalDateTime property, and I can't reproduce. Are you sure you don't have other dependencies that would change the configuration of Jackson?
– JB Nizet
14 hours ago
2
Do you have
@EnableWebMvc
on one of your@Configuration
classes? If so, remove it. It fights with the spring boot way of configuring spring mvc.– teppic
12 hours ago
@JBNizet I think you may be on to something. For this project I'm using mongodb - it may be that the spring-starter-mongo serializer/deserializer is the culprit - I'll get back when I have tried to remove it.
– Martin Jes Rasmussen
4 hours ago
@teppic +1 Bingo that was the problem - now I hope my Cors config still works... Too bad I can't accept your response as a solution. :)
– Martin Jes Rasmussen
4 hours ago