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

I'm new to spring, I'm currently trying to execute an HTTP POST request request / x-www-form-url, but when I keep this in my headers, spring doesn't recognize it and says 415 Unsupported Media Type forx-www-form-urlencoded

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

Can anyone 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;
}
+9
source share
6 answers

, , application/x-www-form-urlencoded, Spring RequestBody. , @RequestBody.

:

@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;
}

, @RequestBody

+34

@RequestBody @RequestParam Java.

, , :

@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;
}
+4

Spring, . "" , "Content-Type".

@RequestMapping(value = "/", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded"})

, .

0

- ajax "application/json; charset = utf-8", api json.

var basicInfo = JSON.stringify(
                    {
                        firstName : playerProfile.firstName(),
                        lastName : playerProfile.lastName(),
                        gender : playerProfile.gender(),
                        address : playerProfile.address(),
                        country : playerProfile.country(),
                        bio : playerProfile.bio()
                    });

 $.ajax({
                    url: "http://localhost:8080/social/profile/update",
                    type: 'POST',
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    data: basicInfo,
                    success: function(data) {



                    }
                });


@RequestMapping(value = "/profile/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> UpdateUserProfile(@RequestBody User usersNewDetails, HttpServletRequest request, HttpServletResponse response){

, , spring , json ajax. . ajax - "application/x-www-form-urlencoded"

0

The solution can be found here https://github.com/spring-projects/spring-framework/issues/22734

You can create two separate mail 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();
}
0
source

replace contentType: "application / x-www-form-urlencoded" with dataType: "text" as wildfly 11 does not support the mentioned content type.

0
source

Source: https://habr.com/ru/post/1624214/


All Articles