We have a usecase where the JSON returned by the endpoint has to be serialized differently based on the endpoint. Is it possible to register two separate ObjectMapper beans and specify which one to use for a specific controller? For example, if I define a custom objectmapper as shown below, can I ask Spring Boot to use this mapper to serialize only the return objects from ControllerTwo but use the default/Primary objectmapper for serializing objects returned from ContorllerOne?
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper mapper = builder.build();
return mapper;
}
@Bean
public ObjectMapper objectMapperCustom(Jackson2ObjectMapperBuilder builder) {
ObjectMapper mapper = builder.build();
//customize mapper
return mapper;
}
@RestController
public class ControllerOne {
@GetMapping("/personNew/{id}")
public Person getMyClass() {
}
}
@RestController
public class ControllerTwo {
@GetMapping("/personOld/{id}")
public Person getMyClass() {
}
}