MappingJackson2HttpMessageConverter isn't using the ObjectMapper configured by a #Bean - spring-boot

i am using spring-boot 3.0.2 and have created a custom jackson serializer and registered it using #JsonComponent. i have confirmed it is registered in the ObjectMapper instance that gets injected using #Autowire. when i go to invoke the rest endpoint that will return an object that should be handled by the custom serializer, it is never by the RequestResponseBodyMethodProcessor.
after doing some digging it seems only the default set of converters created in WebMvcConfigurationSupport are being used and nothing else. i have tried registering my own instance of MappingJackson2HttpMessageConverter as a #Bean but it is never used.
the only way i have found to fix this issue is to replace whatever default is being created using extendMessageConverters in WebMvcConfigurer.
can anyone offer any suggestions? from the various blog posts i've read, this should just work but appears not to.
i expect json that looks like this:
["fqdn":"test.com"]
but instead get:
["fqdn:{"idnEncoded":false,"domainName":"test","tldName":"com"}]

Related

Adding custom Jackson Module to Spring boot's ObjectMapper is ignored

I use SpringBoot v2.2.2RELEASE (very old version) and I configured a custom Module as follow:
#Configuration
public class JacksonConfig {
#Bean
public Module jacksonModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(Object.class, new MyCustomSerializer());
return module;
}
}
This works well and it does what it supposed to do.
Recently, I've upgraded to the lates SpringBoot 2.7.0 and the serializer gets ignored! I can see that when my app loads then it instantiates a new Module instance, but it doesn't call the serializer anymore...
(What the serializer does is add a root node to the response (json) that returns to the client via REST Controller).
Any idea?
EDIT1
I tried to debug it by put a breakpoint in JacksonAuthConfiguration#configureModules and I saw the module in the list as well as some other Swagger related modules.
So if I understand it correct your problem is that Spring doesnt take this ObjectMapper for the RestController. And in this case I struggled with this problem aswell. I tried everything from defining a primary bean for ObjectMapper, defining a bean for the factor adding the module as you did but nothing worked.
So in the end my solution was to use #JsonDeserialize and #JsonSerialize annotations.

Spring Boot application has about 20 ObjectMappers

I'm trying to configure the Jackson ObjectMapper in a Spring Boot application. I've tried various approaches as suggested here and in the documentation (e.g. using a Jackson2ObjectMapperBuilder or Jackson2ObjectMapperBuilderCustomizer #Bean). I want to register a mixin to exclude some fields in the DefaultMessageSourceResolvable class, which should be fairly straightforward.
However, by placing a breakpoint in the ObjectMapper constructor, I've seen that there are about 20 instances being created. Multiple instances of Jackson2ObjectMapperBuilder are created as well.
Creating a bean of the Jackson2ObjectMapperBuilderCustomizer type does not work either; it gets invoked, but the mixin is not registered in the instance which is eventually used for serialization.
Is it normal to have so many instances of these classes? If it is, what might be the reason that my customization efforts fail? If not, what might be wrong and how may it be fixed?

Spring 4 Rest RequestMappingHandlerAdapter not Saving Configured MessageConverters

I am having a problem with configuring the RequestMappingHandlerAdapter; which is used in a Spring 4.1.4 Restful WebService configuration. When I configure the RequestMappingHandlerAdapter message converters, it doesn't not use the message converters that I've configured. I put break points in the RequestMappingHandlerAdapter.setMessageConverters(List<HttpMessageConverter<?>> messageConverters) method and on application startup I see this method being called three times. The first two times this method is called it has the pre-configured message converters, one of which is the Jaxb2RootElementHttpMessageConverter. On the third time, this method is called with my manually configured message converters via application-context.xml bean configuration. At this point, I am thinking that I have successfully reset the message converters with my own configuration; but that is not so because when I invoke my Restful WebService, Spring is calling the Jaxb2RootElementHttpMessageConverter instead of the MarshallingHttpMessageConverter that I manually configured via application-context.xml.
So I need to know how to:
How to tell Jaxb2RootElementHttpMessageConverter to use my configured JAXB2Marshaller; which is configured to work with JAXBIntroductions,
Unregister the Jaxb2RootElementHttpMessageConverter in Spring 4.1.4,
Tell Spring 4.1.4 when it see XML data to use MarshallingHttpMessageConverter instead of the Jaxb2RootElementHttpMessageConverter,
Create my own custom version of Jaxb2RootElementHttpMessageConverter so I can give it the correct JAXB2 Marshaller; which is configured to work with JAXBIntroductions, or
Get the RequestMappingHandlerAdapter to only used the configuration that I give it.
Any help with any of the five options above would be greatly appreciated.
Thank you.
Tonté
I too faced same issue.
You have to remove from the context file.
Its overriding the converters even if we specified list of converters.
I too faced same issue.
You have to remove mvcannotationDriven from the context file.
Its overriding the converters even if we specified list of converters.

Spring Boot and Jackson, JSR310 in response body

The JacksonAutoConfiguration initializes an ObjectMapper with the JSR310Module registered. Then HttpMessageConvertersAutoConfiguration initializes a MappingJackson2HttpMessageConverter. But this MessageConverter never gets plugged in the RequestResponseBodyMethodProcessor.
As a result:
If the controller returns a object which should be jsonified by Jackson, dates are poorly converted because the wrong MappingJackson2HttpMessageConverter is used (initialized in WebMvcConfigurationSupport#addDefaultHttpMessageConverters)
But, if the ObjectMapper is injected in the controller and used to jsonify the same object, then the date format is alright.
How can I can wake the WebMvcAutoConfigurationAdapter up? What should I do to bind the configured Jackson ObjectMapper with Spring MVC and make it handle response bodies?
If you use #EnableWebMvc that would explain your symptoms So the rule thumb is: do not use #EnableWebMvc with Spring Boot, unless you wan't to customize Web MVC configuration?

Spring-Boot 0.5.0.BUILD-SNAPSHOT and customer Json MappingJackson2HttpMessageConverter

Has anyone working with Spring-Boot figured out how to override the default Jackson ObjectMapper that is automatically configured? I have tried to instantiate both the ObjectMapper as a bean and within a MappingJackson2HttpMessageConverter also as a been. But with no success...
Any thoughts?
Spring handles HTTP #ResponseBody using the HttpMessageConverter abstraction, so it's there that you would have to add customizations. Normally you would add a #Bean extending WebMvcConfigurerAdapter and override the configureMessageConverters() method.
Adding a #Bean of type ObjectMapper sounds like a neat way to do this for a Spring Boot application, so you could provide that feature in a pull request if you like.

Resources