Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Is ObjectMapper thread-safe? How should it be used in a service?

Yes, ObjectMapper is thread-safe once it is fully configured: many threads can read and write JSON with the same instance at the same time. We should create it once (in Spring, inject the one Boot provides) and reuse it, because a new mapper per call throws away its expensive caches. We must not change its configuration while it is in use; for per-call variations we use ObjectReader and ObjectWriter, which are immutable and cheap to create.

Why this question is asked

ObjectMapper looks like an ordinary helper object, so people create it wherever they need it, often inside a method that runs thousands of times per second. Others share one instance but then call configure() on it in the middle of a request to turn on pretty printing for one response, and wonder why another request suddenly gets indented JSON. Interviewers ask this question because it checks whether we know what thread safety really means for a configurable object, and how to vary behaviour without sharing mutable state. In this answer, we will see what makes ObjectMapper safe to share, why creating one per call is slow, what goes wrong when configuration changes at runtime, and how ObjectReader and ObjectWriter give per-call variants safely.

1What Thread-Safe Means for ObjectMapper

An object is thread-safe when several threads can use it at the same time without corrupting it or seeing wrong results. For ObjectMapper, Jackson's documentation promises exactly this for reading and writing: readValue and writeValueAsString can be called concurrently from any number of threads, as long as all configuration happened before the mapper was shared.

The reason is how Jackson works internally. The mapper holds its configuration in immutable config objects, and it keeps caches of serializers and deserializers, the small objects that know how to handle a particular class. Those caches are built to be read and filled concurrently. Each call creates its own short-lived context object for the work it does, so threads do not step on each other's state.

2Why Creating One per Call Is Slow

The first time a mapper sees a class, it introspects it: it finds the properties, reads annotations, decides on naming and builds a serializer. That is the expensive part, and the result is cached in that mapper. A shared mapper pays the cost once per class for the life of the application. A mapper created per call pays it on every call and then throws the cache away.

Java
// Slow: a new mapper and a cold cache on every request
String toJson(Order order) throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(order);
}
// Fast: one mapper, built once, shared by every thread
@Service
class OrderJson {
private final ObjectMapper mapper; // Boot's mapper, injected
OrderJson(ObjectMapper mapper) { this.mapper = mapper; }
String toJson(Order order) throws JsonProcessingException {
return mapper.writeValueAsString(order); // serializer for Order comes from the cache
}
}
// Rough illustration on a laptop, small Order object:
// new mapper per call: tens of microseconds per call, plus extra garbage
// shared warm mapper: around a microsecond per call

Notice that the numbers are only an illustration; the real gap depends on the class and the JVM, but it is consistently large, often an order of magnitude. In a Spring application there is a second reason to inject the mapper: Boot's mapper has the application's modules and settings, and a new ObjectMapper() has none of them, so its output may not even match what the API returns elsewhere.

3What Goes Wrong When Configuration Changes in Use

The promise of thread safety covers reading and writing, not reconfiguring. Methods such as configure(), enable(), setPropertyNamingStrategy() and registerModule() change the shared configuration, and Jackson does not coordinate those changes with calls already running. Some settings are also baked into serializers that are already cached, so a change may apply to some classes and not others.

Java
// Broken: one request toggles pretty printing on the shared mapper
String export(Report r, boolean pretty) throws JsonProcessingException {
mapper.configure(SerializationFeature.INDENT_OUTPUT, pretty); // changes it for every thread
return mapper.writeValueAsString(r);
}
// Thread A: export(r, true) -> sets INDENT_OUTPUT on
// Thread B: normal API call -> its response is suddenly indented
// Thread A: export(r2, false) -> may still be indented if B switched it back on in between
Step throughTwo requests sharing a reconfigured mapper
Thread AShared ObjectMapperThread B
configure(INDENT_OUTPUT, true)
writeValueAsString(apiResponse)
indented JSON (wrong for B)
writeValueAsString(report)
indented JSON

5 messages in order. Play them, or step through them one at a time.

Configuration is shared state; thread B was never asked.

Notice that nothing crashes; the output is just wrong now and then, which is the hardest kind of bug to trace. The rule is simple: configure the mapper completely at startup, then treat it as read-only. In Jackson 3, which Spring Boot 4 uses, the mapper is immutable by design: configuration happens through a builder, and changing a setting means building a new mapper with rebuild(), which makes this mistake impossible to write.

4Per-Call Variants With ObjectReader and ObjectWriter

Jackson's answer to per-call options is ObjectReader and ObjectWriter. We get them from the shared mapper, and each one is an immutable object holding the mapper's configuration plus our changes. Every with or without method returns a new instance, so we can create variants freely, cache the common ones in fields, and share those across threads too.

Java
@Service
class ReportJson {
private final ObjectWriter compact;
private final ObjectWriter pretty;
private final ObjectReader lenientOrder;
ReportJson(ObjectMapper mapper) {
this.compact = mapper.writer();
this.pretty = mapper.writerWithDefaultPrettyPrinter(); // new object, mapper unchanged
this.lenientOrder = mapper.readerFor(Order.class)
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
String export(Report r, boolean indent) throws JsonProcessingException {
return (indent ? pretty : compact).writeValueAsString(r); // no shared state changes
}
Order readLegacy(String json) throws IOException {
return lenientOrder.readValue(json);
}
}
// export(r, true) -> indented JSON for this call only
// export(r, false) -> compact JSON; other threads are never affected

Notice that the readers and writers share the mapper's caches, so they keep its speed. They also cover views (writerWithView), a specific root type (writerFor) and a different date format (with(DateFormat)), which together handle almost every case where people are tempted to reconfigure the mapper.

5Follow-up questions

Is it safe to register a module after the application has started?

Not while other threads are using the mapper. Registering a module changes the configuration and may not affect serializers that are already cached. Modules belong in startup configuration, which in Spring Boot means a Module bean or a builder customizer.

Are JsonParser and JsonGenerator thread-safe too?

No. A parser or generator belongs to one read or write operation and holds its current position, so it must stay on one thread. The mapper, readers and writers create a fresh one for each call, which is why they can be shared and these cannot.

Should I keep a static ObjectMapper in a utility class?

It is thread-safe, so it works, but in a Spring application it bypasses Boot's configuration and gives two different JSON behaviours. Injecting Boot's mapper, or a reader or writer made from it, keeps one consistent configuration and makes tests easier.

6Conclusion

In this answer, we saw that ObjectMapper is thread-safe for reading and writing once it is configured, because its configuration is held in immutable objects and its caches are built for concurrent use. Creating one per call throws away those caches and is many times slower, and in Spring it also loses Boot's configuration. Changing configuration while the mapper is shared leaks one request's settings into others, so we configure it once at startup, and for per-call needs we use ObjectReader and ObjectWriter, which are immutable, cheap and safe to share. Jackson 3 enforces this by making the mapper itself immutable.

What to say in the interviewAbout 90 seconds aloud

Open with

Yes, ObjectMapper is thread-safe, but with a condition: once it is configured. Reading and writing from many threads with the same instance is fine, because its configuration sits in immutable config objects and its serializer and deserializer caches are designed for concurrent access.

Then, if they want more

So the right way to use it is to create it once and reuse it, and in Spring that means injecting the mapper Boot already built, which also has the application's modules and settings.

Creating a new ObjectMapper per call is slow, because every new mapper has to introspect each class and build its serializers again; a warm shared mapper gets them from its cache, so it's usually an order of magnitude faster.

What is not safe is changing configuration while it's in use, for example calling configure to turn on pretty printing for one request. That changes it for every thread, so another request may suddenly get indented output, and some settings don't even reach cached serializers.

For per-call differences I use ObjectReader and ObjectWriter, which I get from the mapper with writer(), writerWithDefaultPrettyPrinter(), readerFor() and the with and without methods. They're immutable, cheap, share the mapper's caches, and I can keep them in fields. In Jackson 3, with Spring Boot 4, the mapper itself is immutable and built through a builder, so the problem goes away.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Is ObjectMapper thread-safe? How should it be used in a service?": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/81bd583b76/