Http Post request with content type application/x-www-form-urlencoded not working in Spring
The problem is that when we use application/x-www-form-urlencoded, Spring doesn't understand it as a RequestBody. So, if we want to use thiswe must remove the @RequestBody annotation.
Then try the following:
@RequestMapping(value = "/patientdetails", method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)public @ResponseBody List<PatientProfileDto> getPatientDetails( PatientProfileDto name) { List<PatientProfileDto> list = new ArrayList<PatientProfileDto>(); list = service.getPatient(name); return list;}
Note that removed the annotation @RequestBody
you should replace @RequestBody with @RequestParam, and do not accept parameters with a java entity.
Then you controller is probably like this:
@RequestMapping(value = "/patientdetails", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})public @ResponseBody List<PatientProfileDto> getPatientDetails( @RequestParam Map<String, String> name) { List<PatientProfileDto> list = new ArrayList<PatientProfileDto>(); ... PatientProfileDto patientProfileDto = mapToPatientProfileDto(mame); ... list = service.getPatient(patientProfileDto); return list;}
The solution can be found here https://github.com/spring-projects/spring-framework/issues/22734
you can create two separate post request mappings. For example.
@PostMapping(path = "/test", consumes = "application/json")public String test(@RequestBody User user) { return user.toString();}@PostMapping(path = "/test", consumes = "application/x-www-form-urlencoded")public String test(User user) { return user.toString();}