1

I am trying to use JMSTemplate to publish a JSON message to a topic. This code already existed in one application and I was simply copying it to another as we are trying to consolidate two applications into one. I have found that the code is now sending JSON messages that have the first letter capitalized for the JSONArray and JSONObject field names.

I was using JMS template with a message converter that takes in an object mapper to convert from a POJO to a JSON. The only real difference in my new code is that I am using a newer version of spring boot. I know this would update all of the jackson dependencies so maybe that is why this change has occurred. I ended up trying to set the naming strategy on my object mapper but this doesn't seem to work. I originally did it in my bean definition but in order to see if it was actually working I tried it before I did a convertAndSend, and it did not work. I was still getting uppercase JSON Object and Array names.

public void sendMessage(Object responseToSend) {

    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);// does not seem to make a difference
    try {
        System.out.println(objectMapper.writeValueAsString(responseToSend));//prints array and object names with the first letter capitolized
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    jmsTemplatePublish.convertAndSend("REDACTED",
            responseToSend);
}

So, For example, my new application is sending something like.

"Quote":[{"QuoteInformation":{"Inputs":{"exampleField":false,"ExampleWritten":{"dwelling":true}}

where before it was like this

"quote":[{"quoteInformation":{"inputs":{"exampleField":false,"exampleWritten":{"dwelling":true}}
Naveen
  • 360
  • 1
  • 8
  • 23
Kachopsticks
  • 125
  • 1
  • 13

2 Answers2

0

@Kachopsticks did you tried that PropertyNamingStrategy.LOWER_CASE in objectMapper namingStrategy configs instead of using PropertyNamingStrategy.LOWER_CAMEL_CASE.

  • So interesting behavior. I made the change as you suggested and I see all of the string field names being changed to all lower case, but names for my JSONArray and JSONObjects remain upper case camel. So does PropertyNamingStrategy only apply to fields and not Arrays or Objects? – Kachopsticks Oct 07 '19 at 18:48
0

This bean was the culprit. Had to remove .modulesToInstall(JaxbAnnotationModule.class);

@SuppressWarnings("unchecked")
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    return Jackson2ObjectMapperBuilder.json()
            .serializationInclusion(JsonInclude.Include.NON_EMPTY)
            .defaultViewInclusion(true)
            .modulesToInstall(JaxbAnnotationModule.class);
}
Kachopsticks
  • 125
  • 1
  • 13