I'm using latest Spring Boot (1.2.1) and whatever Spring MVC version comes with it.
I have a controller method with implicit JSON conversions for both incoming and outgoing data:
@RestController
public class LoginController {
@RequestMapping(value = "/login", method = POST, produces = "application/json")
ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
// ...
}
}
This works fine, but only if request Content-Type
is set to application/json
. In all other cases, it responds with 415, regardless of the request body:
{
"timestamp": 1423844498998,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/login/"
}
Thing is, I'd like to make my API more lenient; I want Spring to only use the POST request body and completely ignore Content-Type
header. (If request body is not valid JSON or cannot be parsed into LoginRequest instance, Spring already responds with 400 Bad Request which is fine.) Is this possible while continuing to use the implicit JSON conversions (via Jackson)?
I've tried consumes="*"
, and other variants like consumes = {"text/*", "application/*"}
but it has no effect: the API keeps giving 415 if Content-Type
is not JSON.
Edit
It looks like this behaviour is caused by MappingJackson2HttpMessageConverter whose documentation says:
By default, this converter supports
application/json
andapplication/*+json
. This can be overridden by setting thesupportedMediaTypes
property.
I'm still missing how exactly do I customise that, for example in a custom Jackson2ObjectMapperBuilder...