Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http Post request with content type application/x-www-form-urlencoded not working in Spring

Am new to spring currently am trying to do HTTP POST request application/x-www-form-url encoded but when i keep this in my headers then spring not recognizing it and saying 415 Unsupported Media Type for x-www-form-urlencoded

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported

Can any one know how to solve it? please comment me.

An example of my controller is:

@RequestMapping(     value = "/patientdetails",     method = RequestMethod.POST,     headers="Accept=application/x-www-form-urlencoded") public @ResponseBody List<PatientProfileDto> getPatientDetails(         @RequestBody PatientProfileDto name ) {      List<PatientProfileDto> list = new ArrayList<PatientProfileDto>();     list = service.getPatient(name);     return list; } 
like image 640
Rajesh Avatar asked Jan 14 '16 04:01

Rajesh


1 Answers

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 this we 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

like image 112
Douglas Ribeiro Avatar answered Sep 20 '22 15:09

Douglas Ribeiro