CodexBloom - Programming Q&A Platform

Spring MVC: Getting 415 Unsupported Media Type When Submitting Form with Custom Content-Type Header

👀 Views: 83 💬 Answers: 1 📅 Created: 2025-06-30
spring-mvc json http rest Java

I'm stuck trying to I'm reviewing some code and This might be a silly question, but I've looked through the documentation and I'm still confused about I'm working with an scenario with my Spring MVC application where I receive a `415 Unsupported Media Type` behavior when submitting a form that includes a custom `Content-Type` header. The form is meant to send JSON data to my endpoint. Here’s what I have: The controller method is defined as follows: ```java @PostMapping(value = "/submit", consumes = "application/vnd.myapp+json") public ResponseEntity<String> submitData(@RequestBody MyData data) { // Process the data return ResponseEntity.ok("Success"); } ``` When I submit the form using JavaScript, I set the header like this: ```javascript fetch('/submit', { method: 'POST', headers: { 'Content-Type': 'application/vnd.myapp+json' }, body: JSON.stringify({ name: 'Test', age: 30 }) }); ``` Despite sending the correct `Content-Type` header, I still encounter the behavior. I've double-checked that `MyData` is properly annotated for JSON serialization. My application is using Spring MVC version 5.3.20, and I have Jackson configured for JSON processing. Here’s the `pom.xml` dependency snippet for Jackson: ```xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> </dependency> ``` To troubleshoot, I’ve added a custom `HttpMessageConverter` in my configuration: ```java @Bean public HttpMessageConverter<Object> myMessageConverter() { ObjectMapper objectMapper = new ObjectMapper(); // Custom configurations if needed return new MappingJackson2HttpMessageConverter(objectMapper); } ``` However, the question continues. I’ve also tried changing the header to `application/json` and adjusting my controller method’s `consumes` attribute, but it still returns the same behavior. I need to ensure that my application can handle this custom content type. What could be the question here? What's the best practice here? I'm working on a CLI tool that needs to handle this. Any help would be greatly appreciated! I'm using Java 3.9 in this project. I'd love to hear your thoughts on this. For reference, this is a production CLI tool. This is part of a larger application I'm building.