Content type 'application / x-www-form-urlencoded; charset = UTF-8 'not supported for @RequestBody MultiValueMap
Based on the answer to the x-www-form-urlencoded issue with Spring @Controller
I wrote below @Controller method below
@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST
, produces = {"application/json", "application/xml"}
, consumes = {"application/x-www-form-urlencoded"}
)
public
@ResponseBody
Representation authenticate(@PathVariable("email") String anEmailAddress,
@RequestBody MultiValueMap paramMap)
throws Exception {
if(paramMap == null || paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
}
a request that fails with the error below
{
"timestamp": 1447911866786,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/users/usermail%40gmail.com/authenticate"
}
[PS: Jersey was much friendlier, but couldn't use it now given the practical limitations here]
source to share
The problem is that when we use application / x-www-form-urlencoded , Spring doesn't understand it as RequestBody. So, if we want to use this we must remove the @RequestBody annotation .
Then try this:
@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Representation authenticate(@PathVariable("email") String anEmailAddress, MultiValueMap paramMap) throws Exception {
if(paramMap == null && paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
return null;
}
Please note that the @RequestBody annotation has been removed
answer: Http post request with content type / x -www-form-urlencoded app doesn't work in Spring
source to share
I wrote an alternative to fooobar.com/questions/12477717 / ... .
There I wrote step by step explaining with code. Short way:
First : write an object
Second : create a converter to display a model extending AbstractHttpMessageConverter
Third : ask spring to use this converter implementing WebMvcConfigurer.class overriding configureMessageConverters method
Fourth and last, when you use this parameter, implementations in the display inside your controller will consume = MediaType.APPLICATION_FORM_URLENCODED_VALUE and @RequestBody in front of your object.
I am using spring boot 2.
source to share
Instead of using a map, you can use the parameters directly:
@RequestMapping(method = RequestMethod.POST, value = "/event/register")
@ResponseStatus(value = HttpStatus.OK)
public void registerUser(@RequestParam(name = EVENT_ID) String eventId,
@RequestParam(name = ATTENDEE_ID) String attendeeId,
@RequestParam(name = SCENARIO) String scenario) {
log.info("Register user: eventid: {}, attendeeid: {}, scenario: {} ", eventId,attendeeId,scenario);
//more code here
}
source to share