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

后端 未结 7 1593
逝去的感伤
逝去的感伤 2020-11-27 06:19

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 an

相关标签:
7条回答
  • 2020-11-27 06:58

    You have to tell Spring what input content-type is supported by your service. You can do this with the "consumes" Annotation Element that corresponds to your request's "Content-Type" header.

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

    It would be helpful if you posted your code.

    0 讨论(0)
  • 2020-11-27 07:05

    The easiest thing to do is to set the content type of your ajax request to "application/json; charset=utf-8" and then let your API method consume JSON. Like this:

    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
    ) {
        // ...
    }
    

    I guess the problem is that Spring Boot has issues submitting form data which is not JSON via ajax request.

    Note: the default content type for ajax is "application/x-www-form-urlencoded".

    0 讨论(0)
  • 2020-11-27 07:06

    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

    0 讨论(0)
  • 2020-11-27 07:06

    Remove @ResponseBody annotation from your use parameters in method. Like this;

       @Autowired
        ProjectService projectService;
    
        @RequestMapping(path = "/add", method = RequestMethod.POST)
        public ResponseEntity<Project> createNewProject(Project newProject){
            Project project = projectService.save(newProject);
    
            return new ResponseEntity<Project>(project,HttpStatus.CREATED);
        }
    
    0 讨论(0)
  • 2020-11-27 07:08

    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();
    }
    
    0 讨论(0)
  • 2020-11-27 07:12

    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;
    }
    
    0 讨论(0)
提交回复
热议问题