0

I'm using Spring Integration + Spring Cloud Stream to create the following flow:

@Bean
public IntegrationFlow testEventFlow(){
    return IntegrationFlows.from(TestEventSink.INPUT)
        .transform(Transformers.fromJson(TestEvent.class))
        .transform(TestEvent::getSomeId)
        .log()
        .channel("nullChannel")
        .get();
}

Test event class is:

public class TestEvent {

  private String someId;

  public TestEvent(){}

  public TestEvent(String someId){
      this.someId = someId;
  }

  public String getSomeId(){
      return someId;
  }

  public void setSomeId(String someId){
      this.someId = someId;
  }
}

Problem here is that messages from TestEventSink.INPUT are coming in snake case:

{ "some_id" : "1234" }

If I annotate Test Event class with @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) it works well, but I can't annotate the class because it's from a third party library.

I tried to set the property spring.jackson.property-naming-strategy=SNAKE_CASE, but It doesn't worked either.

I also tried:

@Bean
public ObjectMapper objectMapper(){
    ObjectMapper mapper = new ObjectMapper();
    mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    return mapper;
}

But it doesn't worked.

I think that I can use a custom mapper for that transformation, but I'm looking for something that changes the configuration of all Spring Integration JSON transformers at same time.

Thanks in advance!

italktothewind
  • 1,950
  • 2
  • 28
  • 55
  • With `PropertyNamingStrategy.SNAKE_CASE` your ObjectMapper will work if you call it like this: `mapper.readValue(str, SomeClass.class);` . where str is the snake-cased-json-as-string and SomeClass is the POJO mapping to the JSON. – raviiii1 Apr 07 '19 at 08:31

1 Answers1

2

You are missing the fact to inject auto-configured by Spring Boot ObjectMapper into the fromJson() factory:

. transform(Transformers.fromJson(TestEvent.class), 
       new Jackson2JsonObjectMapper(objectMapper))

The point is that Spring Integration comes before Spring Boot, but not vice versa, therefore the auto-configuration is not visible in Spring Integration as is.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118