Profile based, toggleable JSON minifying with Java, Spring

With Jackson, the annotation @JsonProperty("a")

in the field will serialize its name as a

. With Gson, this is done like @SerializedName("a")

.

Annotating all the serialized fields of the class and going with Jackson's default setting will result in a mini JSON response, which is what we want to achieve.

This is clearly obscuring the answer, so it would be great to make it optional. With non-production profiles, minification should be disabled, while the Production profile should use the reduced version.

The app uses Spring Boot 2 (REST + Data) and Java 8 and is currently serializing with Jackson, but that may change. The assembly is done through Maven.

We can use Spring or Maven profiles for this.

Speculation:

  • Ditch annotations and configure Jackson / Gson with beans profile. Not sure if it's possible.

  • Jackson's annotations are not used at this time. I could implement my own implementation from an external module and switch between a dummy and a real one based on the profile, hoping that mine won't auto-configure. Dirty and may not work.

  • Duplicate answer objects, but this is ugly and difficult to maintain.

Anyway, the question is how to make JSON a config-based mini setting toggleable.

+3


source to share


3 answers


Indenting is not a problem. Maybe my question is not clear. The problem is to minimize the field names. Non-prod, I would like to have "someLongFieldName": 1. With prod profile, "s": 1.



You can use a placeholder. Use @JsonProperty("${your.property}")

instead @JsonProperty("a")

. Spring boot will evaluate the expression and replace it with the appropriate string.

+1


source


So, let's say you have dev and prod profiles. To disable JSON minifying for the dev profile, just add the application-dev.properties file using config:

spring.jackson.serialization.indent_output=true

      



or with Java config:

@Configuration
@Profile("dev")
public class DevJacksonConfig {
    @Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.indentOutput(true);
        return builder;
    }
}

      

0


source


I don't know what you need to do, but I think of it as a code smell.

Using different keys according to the environment (in our case dev and prod, but it could be more) makes your APIs different depending on the environment. However, the content is actually the same. This way you will have a different response for the same request according to the deployed API profile.

Any API consumer should then know the true meaning of key obfuscation. Worse, they will have to handle both obfuscated and "regular" keys in order to be able to communicate with the API no matter which profile is deployed.

It will also make it easier to test and develop API docs (swagger, etc.).

I would highly recommend sticking to a well-named key and using response compression (like gzip format).

0


source







All Articles