Quick revision
The short answer
The runtime negotiates a media type from the method's @Produces and the client's Accept header, then picks a MessageBodyWriter whose isWriteable accepts the Java type and that media type, and calls its writeTo to stream the JSON. Jakarta EE standardises JSON-B (implemented by Yasson) and JSON-P for this, and a full application server ships them; with Jersey we add jersey-media-json-binding or jersey-media-json-jackson, and with RESTEasy resteasy-json-binding-provider or resteasy-jackson2-provider. Without a suitable writer the request fails with a 500 such as MessageBodyProviderNotFoundException; custom providers are registered with @Provider or explicitly through Application.getClasses or ResourceConfig.register.
What to remember
In this answer, we saw that JAX-RS turns a returned OrderDto into JSON by negotiating the media type from @Produces and the Accept header and then choosing a MessageBodyWriter whose isWriteable accepts the type, before calling writeTo. Application servers provide JSON-B and JSON-P, Jersey needs jersey-media-json-binding or jersey-media-json-jackson, and RESTEasy needs its JSON-B or Jackson provider. Without a writer we get a 500 after the method ran, and custom providers are added with @Provider or explicit registration.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The runtime negotiates a media type from the method's @Produces and the client's Accept header, then picks a MessageBodyWriter whose isWriteable accepts the Java type and that media type, and calls its writeTo to stream the JSON. Jakarta EE standardises JSON-B (implemented by Yasson) and JSON-P for this, and a full application server ships them; with Jersey we add jersey-media-json-binding or jersey-media-json-jackson, and with RESTEasy resteasy-json-binding-provider or resteasy-jackson2-provider. Without a suitable writer the request fails with a 500 such as MessageBodyProviderNotFoundException; custom providers are registered with @Provider or explicitly through Application.getClasses or ResourceConfig.register.
Why this question is asked
Returning an object from a resource method and getting JSON feels like magic until it stops working after a dependency change and every endpoint returns 500. Knowing the provider mechanism lets us fix that in minutes and add our own formats, such as CSV. Interviewers ask this to see whether we understand the plug-in architecture of JAX-RS rather than only its annotations. In this answer, we will recap entity providers, follow one response through content negotiation and writer selection, see what each runtime needs on the classpath, look at the error we get without a writer, and write a small custom provider.
1A Quick Recap of Entity Providers
In Jakarta REST, the code that converts between Java objects and HTTP bodies is not in the runtime core. It is in entity providers: a MessageBodyReader turns a request body into a Java object, and a MessageBodyWriter turns a returned Java object into a response body. Each provider declares which media types it handles with @Consumes or @Produces on the provider class, and answers a question at runtime: can you handle this type in this media type?
The runtime ships with writers for simple types such as String, byte[], InputStream and File. Anything else, including our OrderDto as JSON, needs a provider that knows the format, which is why JSON support is a separate module.
2Following One Response Through the Runtime
Suppose the method is annotated @Produces(MediaType.APPLICATION_JSON) and returns an OrderDto, and the client sends Accept: application/json. First the runtime determines the response media type by intersecting what the method can produce with what the client accepts, using the q-values in the Accept header for preference. Here that is application/json.
Next it collects the writers whose own @Produces is compatible with application/json and asks each one isWriteable(OrderDto.class, genericType, annotations, application/json). A JSON-B or Jackson writer answers true for almost any type. When several answer true, the runtime prefers the closest media type match and the closest Java type match, and providers supplied by the application before the runtime's built-in ones. Then it calls writeTo, which serialises the object straight into the response output stream.
- Method returns OrderDto
@Producesapplication/json - Negotiate media typeIntersect
@Produceswith Accept - Collect writers for that media typeFrom registered and built-in providers
- Any isWriteable() returns true?YesSort by media type and Java type closenessNo500: no writer found
- Call writeTo()Serialise into the output stream
5 steps in order. Play them, or step through them one at a time.
3What Each Runtime Needs on the Classpath
On a full Jakarta EE application server such as WildFly, Open Liberty or Payara, JSON-B and JSON-P are part of the platform, so returning a DTO as JSON works with no extra dependency. JSON-B is the standard object binding API, Yasson is its reference implementation, and JSON-P is the lower-level standard for parsing and building JSON trees.
Standalone runtimes need a media module. Jersey offers jersey-media-json-binding, which uses JSON-B and Yasson, and jersey-media-json-jackson, which uses Jackson; both register themselves automatically through Jersey's feature auto-discovery when they are on the classpath. RESTEasy offers resteasy-json-binding-provider and resteasy-jackson2-provider. The choice decides the default output format, because JSON-B and Jackson differ in details such as dates and null handling.
4What Happens Without a Writer
If no writer accepts OrderDto in application/json, the runtime cannot produce a body and fails the request with a 500. Jersey logs a message along the lines of MessageBodyWriter not found for media type=application/json, type=class OrderDto, and throws MessageBodyProviderNotFoundException; RESTEasy reports that it could not find a MessageBodyWriter for the response object. The exact wording depends on the version.
Notice that the method itself ran successfully: the order was loaded and returned. The failure happens afterwards, while writing the body. So a log line from the service saying "order loaded" and a 500 to the client is a strong hint that the provider is missing, often because a dependency change removed the JSON module.
5Writing and Registering a Custom Provider
We can add our own writer for any format. Here is one that writes an order as CSV when the client asks for text/csv.
If the resource method declares @Produces({"application/json", "text/csv"}), the client chooses the format with its Accept header, and the runtime picks the JSON writer or ours accordingly. The @Provider annotation lets classpath scanning find the class, but explicit registration is more predictable, because scanning can be disabled or limited in some deployments.
6Follow-up questions
What happens if both JSON-B and Jackson providers are present?
Both writers accept application/json for almost any type, so they tie on media type and Java type, and the outcome depends on registration order, provider priority and the runtime's rules, which have changed between versions. In practice the output format can silently change when a dependency is added. We should keep only one JSON module on the classpath, or register the chosen one explicitly and disable auto-discovery of the other, and cover the output with tests.
Where is configuration such as date formats applied?
Through a ContextResolver provider: a ContextResolver<ObjectMapper> for Jackson or a ContextResolver<Jsonb> for JSON-B. The JSON writer asks for it at runtime and uses our configured mapper instead of a default one.
Does the same mechanism read request bodies?
Yes, with MessageBodyReader and isReadable, selected by the request's Content-Type and the method's @Consumes. A missing reader gives 415 or a 500 depending on whether any method could consume the type.
7Conclusion
In this answer, we saw that JAX-RS turns a returned OrderDto into JSON by negotiating the media type from @Produces and the Accept header and then choosing a MessageBodyWriter whose isWriteable accepts the type, before calling writeTo. Application servers provide JSON-B and JSON-P, Jersey needs jersey-media-json-binding or jersey-media-json-jackson, and RESTEasy needs its JSON-B or Jackson provider. Without a writer we get a 500 after the method ran, and custom providers are added with @Provider or explicit registration.
Open with
JAX-RS itself does not know JSON. It uses entity providers: a MessageBodyWriter converts a returned object into the response body. When my method returns an OrderDto and is annotated @Produces application/json, the runtime first negotiates the media type by intersecting @Produces with the client's Accept header.
Then, if they want more
Then it looks at the registered writers for that media type and calls isWriteable with the Java type, generic type, annotations and media type. Among those that say yes, it prefers the closest media type and type match and application providers over built-in ones, and then calls writeTo, which streams the JSON to the output.
On a full Jakarta EE server, JSON-B with Yasson and JSON-P are part of the platform, so it just works. With Jersey standalone I need a media module, jersey-media-json-binding for JSON-B or jersey-media-json-jackson for Jackson, and with RESTEasy resteasy-json-binding-provider or resteasy-jackson2-provider.
If none is present, the method runs fine but writing the body fails with a 500, in Jersey a MessageBodyProviderNotFoundException saying no writer was found for application/json. For custom formats I implement MessageBodyWriter, annotate it with @Provider and @Produces, and either rely on scanning or register it explicitly in Application.getClasses or ResourceConfig.register.
I would avoid having two JSON providers on the classpath, because which one wins is not obvious.
About this answer
- Last updated
- 12 November 2026
- 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.
- Official documentation
- Jakarta EE specifications
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 "How does a JAX-RS runtime turn a returned OrderDto into a JSON response body? Explain...": https://scrivio-interview-library.netlify.app/java/jax-rs/61640d1081/