CodexBloom - Programming Q&A Platform

Spring Boot REST API: 415 Unsupported Media Type when sending XML payload

👀 Views: 10 💬 Answers: 1 📅 Created: 2025-06-29
spring-boot rest xml jackson java

I'm migrating some code and Could someone explain I'm currently working on a Spring Boot REST API that is designed to accept both JSON and XML payloads, but I'm working with a `415 Unsupported Media Type` behavior when I send an XML request. I've configured my application to support XML by adding the necessary dependencies, specifically `jackson-dataformat-xml`, but it seems like my controller method is not recognizing the XML content type properly. Here's the relevant code snippet for my controller: ```java @RestController @RequestMapping("/api/items") public class ItemController { @PostMapping(consumes = { "application/json", "application/xml" }) public ResponseEntity<Item> createItem(@RequestBody Item item) { // Logic to save item return ResponseEntity.status(HttpStatus.CREATED).body(item); } } ``` In my `pom.xml`, I have the following dependency added: ```xml <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.12.3</version> </dependency> ``` I also ensured that my `Item` class is annotated correctly with `@XmlRootElement` to facilitate XML deserialization: ```java @XmlRootElement public class Item { private Long id; private String name; // Getters and setters } ``` When I send a request with the following XML payload: ```xml <item> <id>1</id> <name>Test Item</name> </item> ``` with the header `Content-Type: application/xml`, I still receive the `415 Unsupported Media Type` behavior. I’ve tried debugging by checking the request headers and the payload format, and everything seems to be in order. What could be causing this scenario? Is there something I’m missing in the configuration or any additional annotations that I should be using to ensure that Spring Boot properly recognizes the XML input? What am I doing wrong? I'm working on a service that needs to handle this. I'm open to any suggestions.