Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.35k stars 1.99k forks source link

[BUG] Azure Service Bus Spring Cloud Stream isolated-object-mapper property #41932

Closed Sheldoras closed 1 week ago

Sheldoras commented 1 month ago

Describe the bug Azure Service Bus for Spring Cloud Stream offers the property isolated-object-mapper see here which, at least from what I gather from the property description and available tests regarding this property, should enable a consumer of the library to define their own ObjectMapper to be used for the ServiceBusMessageConverter in place of a default one.

Exception or Stack Trace

2024-09-19T17:12:57.354+02:00  WARN 27448 --- [demo] [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'inputBindingLifecycle'
2024-09-19T17:12:57.360+02:00  INFO 27448 --- [demo] [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-09-19T17:12:57.373+02:00 ERROR 27448 --- [demo] [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method serviceBusMessageConverter in com.azure.spring.cloud.autoconfigure.implementation.servicebus.AzureServiceBusMessagingAutoConfiguration$ServiceBusTemplateConfiguration required a bean of type 'com.fasterxml.jackson.databind.ObjectMapper' that could not be found.

The following candidates were found but could not be injected:
    - User-defined bean method 'myObjectMapper' in 'ConsumerConfig'

Action:

Consider revisiting the entries above or defining a bean of type 'com.fasterxml.jackson.databind.ObjectMapper' in your configuration.

To Reproduce Minimal setup to reproduce Just run the application and it should fail with the error listed above.

Expected behavior My expectation would be that when I set isolated-object-mapper to false - denoting that I do not want the isolated default to be used - but will instead provide my own ObjectMapper as Bean - this custom ObjectMapper is actually used.

Setup (please complete the following information):

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

github-actions[bot] commented 1 month ago

@moarychan @netyyyy @rujche @saragluna

github-actions[bot] commented 1 month ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

moarychan commented 1 week ago

Hi @Sheldoras , thanks for using Service Bus binder!

I can reproducer this issue, the root cause is that the Spring Cloud Stream's sub application context does not contains the custom ObjectMapper bean, there're two workaround to fix it:

  1. Register your custom configuration to the sub context, add below configuration:

    spring:
    cloud:
    stream:
      binders:
        my_bus:
          environment:
            spring:
              main:
                sources: com.example.demo.ConsumerConfig
  2. Share the ObjectMapper bean to sub context, add a file named src/main/resources/META-INF/shared.beans and context is the com.fasterxml.jackson.databind.ObjectMapper.

  3. (Recommend) Use a BinderCustomizer bean to customize the binder, add a below code:

@Bean
BinderCustomizer binderCustomizer() {
    return (binder, binderName) -> {
        ServiceBusMessageChannelBinder serviceBus = (ServiceBusMessageChannelBinder) binder;
        serviceBus.setMessageConverter(new ServiceBusMessageConverter(new ObjectMapper()));
    };
}

Meanwhile, a PR to add shared beans is under review.