Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Spring Boot : Different ObjectMapper instances for Request and Response

I have following controller in my spring boot application:

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<ResponseDto<MyClass> process(@RequestBody RequestDto<MyClass> request){
    return null;
}

MyClass has a field, let's say 'myField' and I want different NamingStrategyconfiguration for request and response for this field (this is because I don't want to create a new class just for one field). I have configured ObjectMapper instance as below:

@Bean
public ObjectMapper objectMapper(){
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setPropertyNamingStrategy(namingStrategy);
    return objectMapper;
}

This will be used both for Request and Response (i.e. deserialization and serialization), is there any way in spring boot by which I can instruct the controller to use different ObjectMapper instances?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can solve it with content negotiation. Firstly, define your custom HttpMessageConverter. In following example I have defined a custom converter that is applied when the request Content-Type header is set to application/test+json:

@Bean
public HttpMessageConverters customConverters() {
    final AbstractJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(new ObjectMapper());
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf("application/test+json")));

    return new HttpMessageConverters(true, Collections.singletonList(converter));
}

For simplicity of this example I've used newly created ObjectMapper - in your case you will have to pass here previously configured object.

Next thing is to tell your action to accept only appliction/test+json requests (keep in mind, that from now on it requires to Content-Type:application/test+json header to present in every request to this endpoint):

@RequestMapping(method = RequestMethod.POST, consumes = "application/test+json")
public ResponseEntity<ResponseDto<MyClass> process(@RequestBody RequestDto<MyClass> request){
    return null;
}

Last thing is to make sure that when you call this endpoint, Content-Type:application/test+json header is set. Of course you can use any other name for desired content type, presented name is just an example.

about 4 years ago · Santiago Trujillo Report

0

You can use a deserialization modifier in your ObjectMapper to override the set of enabled features at object deserialization time via a module. This one should do the trick:

    public class FeatureModifyingBeanDeserializerModifier extends BeanDeserializerModifier {

        private Collection<Class<?>> modifiedClasses;

        public FeatureModifyingBeanDeserializerModifier(Collection<Class<?>> modifiedClasses) {
            this.modifiedClasses = Collections.unmodifiableSet(new HashSet<Class<?>>(modifiedClasses));
        }

        @Override
        public JsonDeserializer<?> modifyDeserializer(
                DeserializationConfig config, BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
            JsonDeserializer<?> result = deserializer;
            Class<?> beanClass = beanDesc.getBeanClass();

            if (modifiedClasses.contains(beanClass)) {
                result = new FeatureModifyingStdDeserializer(deserializer, beanClass);
            }
            return result;
        }

        private static class FeatureModifyingStdDeserializer extends StdDeserializer<Object> {

            private JsonDeserializer<?> deserializer;

            private FeatureModifyingStdDeserializer(
JsonDeserializer<?> deserializer, Class<?> beanClass) {
                super(beanClass);
                this.deserializer = deserializer;
            }

            @Override
            public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
                p.enable(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
                return deserializer.deserialize(p, ctxt);
            }
        }      
    }

You have to register it with the ObjectMapper as a module like this:

ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();

module.setDeserializerModifier(new FeatureModifyingBeanDeserializerModifer(Arrays.asList(Journey.class)));
objectMapper.registerModule(module);

For serialization, you can add an @JsonSerialize annotation to the Journey class and serialize it in whatever way you want. If you need to write an unescaped string you can use writeRaw from JsonGenerator.

about 4 years ago · Santiago Trujillo Report

0

One dirty hack: you may write custom serializer and deserializer for MyClass, there you explicitly use two separate object mapper one for serialization (for response) and second for deserialization (for request).

But it's better to find a way to explicitly customize spring object mapper.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!