Spring Boot Interview – Return JSON and XML from the Same Endpoint (Interactive Q&A)

Spring Boot Interview – Return JSON and XML from the Same Endpoint (Interactive Q&A)

Let’s simulate a Java/Spring Boot interview where the topic is producing both JSON and XML responses from a REST endpoint. Here’s how the conversation might go — beginner-friendly, interactive, and focused on real-world use.


👨‍💼 Interviewer: Suppose you have a REST endpoint that returns a list of Account objects. One client wants JSON, and another wants XML. How would you handle that?

👨‍💻 You: In Spring Boot, I can support both formats from the same method. I don’t need to write two separate endpoints.


👨‍💼 Interviewer: Interesting! So, how does Spring Boot know which format to return?

👨‍💻 You: Spring uses content negotiation. It checks the Accept header in the HTTP request. If the client sends Accept: application/json, it returns JSON. If it sends Accept: application/xml, it returns XML.


👨‍💼 Interviewer: Great. Can you show me how the controller method would look?

👨‍💻 You: Sure!

@RestController
@RequestMapping("/accounts")
public class AccountController {

    @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
    public List<Account> getAccounts() {
        return List.of(new Account(1, "Alice"), new Account(2, "Bob"));
    }
}

👨‍💼 Interviewer: Do I need to annotate the Account class in any special way for XML?

👨‍💻 You: Yes. For XML to work, I need to annotate it with JAXB annotations like @XmlRootElement.

@XmlRootElement
public class Account {
    private int id;
    private String name;
    // Getters, setters, no-arg constructor
}

Also, make sure you have a default (no-arg) constructor — JAXB needs it.


👨‍💼 Interviewer: What if I don’t add JAXB or forget the annotation?

👨‍💻 You: Then Spring Boot will fail to convert to XML and return a 406 Not Acceptable or 500 error depending on the case.


👨‍💼 Interviewer: Do I need to add any dependency for this to work?

👨‍💻 You: For JSON, no extra dependency is needed — Spring Boot uses Jackson. For XML, I need to add the jackson-dataformat-xml dependency:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

👨‍💼 Interviewer: What if one client sends no Accept header? What format will be returned?

👨‍💻 You: JSON is the default format. If no Accept header is present, Spring Boot returns JSON.


👨‍💼 Interviewer: Can I use @RequestMapping instead of @GetMapping to define produces?

👨‍💻 You: Yes, both work:

@RequestMapping(value = "/accounts", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })

But @GetMapping is more readable for GET endpoints.


👨‍💼 Interviewer: What if I want pretty XML output?

👨‍💻 You: You can configure Jackson’s XML mapper or use a custom XmlMapper bean. For example:

@Bean
public XmlMapper xmlMapper() {
    XmlMapper mapper = new XmlMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    return mapper;
}

👨‍💼 Interviewer: Could I return a ResponseEntity instead of a List?

👨‍💻 You: Yes, definitely. This gives more control (e.g., HTTP status, headers).

return ResponseEntity.ok().body(accounts);

Final Tip:

To support both XML and JSON cleanly:

  • Annotate your model with @XmlRootElement
  • Add jackson-dataformat-xml to pom.xml
  • Use produces = { JSON, XML }
  • Spring will handle the rest via content negotiation

Let me know if you want a live demo or test setup too!