Quick question regarding which HttpMessageConverte...
# spring
t
Quick question regarding which HttpMessageConverter is being used by default when using the
org.springframework.web.servlet.function.RouterFunctionDsl
? Because it doesn't seem to be the default configured objectmapper. I just ran into a very annoying problem while livecoding. I was getting json responses where a LocalDateTime was being converted into a [2024,6,4,....] format instead of a "2024-06-04T...." format, which is supposedly the default date-format of spring's configured Jackson ObjectMapper. I ended up injecting the default ObjectMapper and doing conversion myself:
Copy code
ServerResponse.ok()
    .contentType(MediaType.APPLICATION_JSON)
    .body(foundQuotes.toJson())
What am I missing?
k
when you debug the instance of object mapper used, does it have features enabled/disabled as you expect/as is behaving?
t
The workaround I have right now is to use the objectmapper -that Spring provides- directly in my route and that one is indeed configured according to the defaults I expect.
But to directly answer your question, I don’t know which objectmapper, or rather which messageconverter, is used. I also don’t exactly know how to find that information at runtime. There’s a list of messageconverters that belong to a context on a ServerResponse. But it’s difficult to pinpoint which one gets used at runtime when the http response is returned. All I can verify is that it’s not the default objectmapper that Spring configures.
k
ah I misunderstood that code is already having workaround
t
😅
k
I would set breakpoint in AbstractJackson2HttpMessageConverter#writeInternal to see what's going on
❤️ 1
t
It's a different objectMapper instance for sure. The object id's are different. Any idea on identifying where it's coming from?
It's the
defaultObjectMapper
of the
AbstractJackson2HttpMessageConverter
. You'd think it's the same one as the
Copy code
@Primary
@Bean
fun objectMapper() = jacksonObjectMapper()
d
Did you manage to solve the issue? It happened to me as well and same as you I'm totally clueless as to which object mapper is used in the request serialization...
t
The way I solved it was by manually injecting the ObjectMapper that I expected, without having to depend on Spring's default configuration.
d
Ok, so you didn't find the root cause? Because this to me seems as a bug. In my mental model of Spring Boot app the same autoconfigured object mapper should be used everywhere. Or am I mistaken here?
Also, what exactly was your context? Did you have problems with serialization of responses coming from your app? Because mine problem is with serialization of requests to 3rd party REST APIs.
In the case of request body serialization I don't even know how/where to inject the working object mapper...
t
I'll copy paste my original question:
I just ran into a very annoying problem while livecoding.
I was getting json responses where a LocalDateTime was being converted into a [2024,6,4,....] format instead of a "2024-06-04T...." format, which is supposedly the default date-format of spring's configured Jackson ObjectMapper.
So it was indeed on the response, not the request. I think you're correct that it'll likely fail if my api would accept json requests that contain differently formatted time fields
It also feels like a bug to me.
k
Imho you have to configure custom MessageConverter
d
The problem is that the objectMapper is already configured correctly and works, i.e. the output of
objectMapper.writeValueAsString(request.body)
has the dates formatted as expected. It's somewhere deep inside the request processing (before it is sent over the wire) that a misconfigured objectMapper is used.
k
issue is imho that messageConverter is not using "global" objectMapper
d
I'm not using
@EnableWebMvc
, at least not directly. Will keep digging.
t
I was. That indeed fixed the issue. Thank you so much @kqr 🙏